setattr() on "object" instance

Sean DiZazzo half.italian at
Mon Mar 16 05:21:49 CET 2009

Why is it that you can setattr() on an instance of a class that
inherits from "object", but you can't on an instance of "object"

>>> o = object()
>>> setattr(o, "x", 1000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'x'

>>> class Object(object):pass
>>> o = Object()
>>> setattr(o, "x", 1000)
>>> o.x

I notice that the first example's instance doesn't have a __dict__.
Is the second way the idiom?


More information about the Python-list mailing list