Thursday 12 July 2018

Python __setattr__ Method

Cited from the book "Python How to Program"

It is important that method __setattr__ uses an object’s __dict__ attribute to set an object’s attributes. If the statement inside __setattr__ method is used,

self._hour = value

method __setattr__ would execute again, with the arguments "_hour" and value, resulting in infinite recursion. Assigning a value through the object’s __dict__ attribute, however, does not invoke method __setattr__, but simply inserts the appropriate key–value pair in the object’s __dict__.



No comments:

Post a Comment