Today I stumbled upon the following behaviour:
class myobject(object):
"""Should behave the same as object, right?"""
obj = myobject()
obj.a = 2 # <- works
obj = object()
obj.a = 2 # AttributeError: 'object' object has no attribute 'a'
I want to know what is the logic behind designing the language to behave this way, because it feels utterly paradoxical to me. It breaks my intuition that if I create a subclass, without modification, it should behave the same as the parent class.
EDIT: A lot of the answers suggest that this is because we want to be able to write classes that work with __slots__
instead of __dict__
for performance reasons. However, we can do:
class myobject_with_slots(myobject):
__slots__ = ("x",)
obj = myobject_with_slots()
obj.x = 2
obj.a = 2
assert "a" in obj.__dict__ # ✔
assert "x" not in obj.__dict__ # ✔
So it seems we can have both __slots__
and __dict__
at the same time, so why doesn't object
allow both, but one-to-one subclasses do?