You can set attributes on any class with a __dict__
, because that is where they are stored. object
instances (which are weird) and any class that defines __slots__
do not have one:
>>> class Foo(object): pass
...
>>> foo = Foo()
>>> hasattr(foo, "__dict__")
True
>>> foo.bar = "baz"
>>>
>>> class Spam(object):
... __slots__ = tuple()
...
>>> spam = Spam()
>>> hasattr(spam, "__dict__")
False
>>> spam.ham = "eggs"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Spam' object has no attribute 'ham'
>>>