Python 101 : The __setattr__() function and __dict__ object


 The __setattr__() method is called in the background when we assign an attribute to a python object.

Below is our example, the "Car" class:


We see that the __setattr__() method is called twice, once for the "brand" attribute and a second time for the "color" attribute.

We also notice that because we have defined our "own" __setattr__() method, our attributes were not stored in the __dict__ object - which is where the attributes of a class are stored -.

So we will do it manually in our next example:


The below line fills out the __dict__ dictionary:


Comments

Leave as a comment:

Archive