1. 学习
  2. /
  3. 课程
  4. /
  5. Intermediate Object-Oriented Programming in Python

Connected

练习

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.

说明 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.