Ahmad Syukri b <syoc...@gmail.com> wrote: > On Mar 16, 1:21 pm, 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' > > I suppose you cannot set attributes to instances of any built-in > classes, i.e. int(), list() etc.
No, there are some built-in types where you can set additional attributes: e.g. functions. You can set additional attributes on any class which has a __dict__ attribute. When you subclass an existing type your new type will always have a __dict__ unless you define __slots__ and the base class did not already have a __dict__. > Since all classes inherit from object, I suppose the definition can be > as simple as 'class Object:pass', and assignment can be as simple as > 'o.x = 1000' Inheriting from object by default is only true if you are talking about Python 3.x. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list