Ethan Furman wrote: > Greetings! > > Perhaps I woke up too early this morning, but this behaviour has me > baffled: > > Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit > (Intel)] on win32 > Type "help", "copyright", "credits" or "license" for more information. > > --> test = object() > > --> setattr(test, 'example', 123) > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > AttributeError: 'object' object has no attribute 'example' > > Shouldn't setattr() be creating the 'example' attribute? Any tips > greatly appreciated! > It's not just object, and it's not just the setattr() function. It's not even just Python 3.1 for that matter! Later Python 2 implementations have the same issue.
>>> x = 1 >>> setattr(x, 'example', 123) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'int' object has no attribute 'example' >>> x.example = 123 Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'int' object has no attribute 'example' >>> There are limits to what you can do with the built-in types. No matter how hard Python tries to make them look the same, ultimately the built-in types are implemented differently from the types you create yourself. For efficiency reasons the attributes of the built-ins are stored in a different way (that's more accessible to the C implementation) than those of the declared types. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 See PyCon Talks from Atlanta 2010 http://pycon.blip.tv/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS: http://holdenweb.eventbrite.com/ -- http://mail.python.org/mailman/listinfo/python-list