ComeçarComece de graça

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.

Este exercício faz parte do curso

Intermediate Object-Oriented Programming in Python

Ver curso

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

class BankAccount:
  def __init__(self, account_number):
    self.account_number = account_number
  
  # Define a method to be executed when setting attributes
  def ____(self, ____, ____):
    pass
Editar e executar o código