Python 3.1, object, and setattr()

Steve Holden steve at holdenweb.com
Thu Apr 1 10:25:08 EDT 2010


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!
> 
By the way, the "solution" is to create a subclass of object (which in
Python 3 simply requires you to declare a class! In Python 2 you'd have
to explicitly subclass object, or (equivalently) set your class's
metaclass to type with

    __metaclass__ = type


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/




More information about the Python-list mailing list