Sean DiZazzo <half.ital...@gmail.com> wrote: > 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" > itself? > > >>> 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 > 1000 > > I notice that the first example's instance doesn't have a __dict__. > Is the second way the idiom?
The lack of a __dict__ is why you can't set the attribute. I've occasionally wanted to use instances of object as holders of arbitrary attributes and wondered why I couldn't (from a language design perspective). But that was only for testing. In real code I think I'd always want a fully defined class. -- R. David Murray http://www.bitdance.com -- http://mail.python.org/mailman/listinfo/python-list