Take the following example:
from foo import Bar
b = Bar
print(b.__slots__)
#Stack trace, no attribute __slots__
Assume then that we want to add a __slots__
attribute because lots (and lots) of Bar
instances will be created.
from foo import Bar
CBar = Bar
CBar.__slots__ = ["one", "two", "three"]
b = CBar
print(b.__slots__)
#Success
b.four = 0
print(b.four)
#Prints 0....hmmm....
Is it possible to import a class from a module and add __slots__
attribute dynamically?