Sep-03-2023, 03:22 PM
(This post was last modified: Sep-03-2023, 03:22 PM by deanhystad.)
Adding attributes is something you can do with most python objects.
In python everything is an object. Each object is an instance of a class. The class defines what methods (functions) the object can execute, and the __init__() method assigns attributes. But the __init__() method is just a convenience. When you assign a value to an attribute in the __init__() method, you are adding a key/value pair to a dictionary, __dict__. You can add attributes to the object at any time, from anywhere, and they are added to the __dict__. That is all I did in my example. I added a "name" attribute to a DataFrame object's __dict__. The DataFrame class is completely unaware of the new attribute.
Not all objects support adding attributes. Some python classes use __slots__ instead of __dict__. Slots objects use less memory and they are faster than objects that use __dict__, but they are less flexible because their attributes are set by the class (the attribute names, not the values). And some python classes that use __dict__ disable the mechanism that lets you add attributes to an object. But these are in the minority, and most python objects let you add whatever attributes you want whenever you want.
In python everything is an object. Each object is an instance of a class. The class defines what methods (functions) the object can execute, and the __init__() method assigns attributes. But the __init__() method is just a convenience. When you assign a value to an attribute in the __init__() method, you are adding a key/value pair to a dictionary, __dict__. You can add attributes to the object at any time, from anywhere, and they are added to the __dict__. That is all I did in my example. I added a "name" attribute to a DataFrame object's __dict__. The DataFrame class is completely unaware of the new attribute.
Not all objects support adding attributes. Some python classes use __slots__ instead of __dict__. Slots objects use less memory and they are faster than objects that use __dict__, but they are less flexible because their attributes are set by the class (the attribute names, not the values). And some python classes that use __dict__ disable the mechanism that lets you add attributes to an object. But these are in the minority, and most python objects let you add whatever attributes you want whenever you want.
class MostlyEmptyClass: def __init__(self): self.a = 1 x = MostlyEmptyClass() print(x.__dict__) x.b = 2 print(x.__dict__)
Output:{'a': 1}
{'a': 1, 'b': 2}
When adding attributes this way it is wise to first print the dir() to verify you are not stomping all over an existing instance variable OR METHOD. Reading the documentation does not tell you everything about a class.Output:>>> dir(x)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'b']