1. Learn
  2. /
  3. Courses
  4. /
  5. Intermediate Object-Oriented Programming in Python

Connected

Exercise

Customizing attribute assignment with __setattr__

Previously, we used descriptors to implement logic that was run when a specific attribute was retrieved, set, or deleted. But what if we'd like to make sure a certain block of code runs each time any attribute is set? In this exercise, you'll practice doing this with a Python magic method.

Instructions 1/3

undefined XP
    1
    2
    3
  • Define a magic method in the BankAccount class that will be executed when attempting to set an attribute for an object of BankAccount; this method will take parameters self, name, and value.