Why does this not work?

Laurent Pointal laurent.pointal at limsi.fr
Fri Feb 2 08:42:56 EST 2007


alain a écrit :
> On Feb 2, 1:57 pm, Bart Van Loon <bbb... at inGen.be> wrote:
>> It was 2 Feb 2007 04:41:48 -0800, when alain wrote:
>>
>>> I tried the following:
>>> myobj=object()
>>> myobj.newattr=5
>>> results in:
>>> Traceback (most recent call last):
>>>   File "<interactive input>", line 1, in ?
>>> AttributeError: 'object' object has no attribute 'newattr'
>>> Any idea?
>> I think it's because... object has no attribute 'newattr'
>>
>> what else is there left to say?
>>
>> try:
>>
>> myobj=object()
>> print dir(myobj)
>>
>> does that contain 'myattr'?
>>
>> --
>> groetjes,
>> BBBart
>>
>>    "To make a bad day worse, spend it wishing for the impossible." -Calvin
> 
> 
> What about this:
> class Object(object):pass
> myobj=Object()
> myobj.newattr=5
> 
> and it works !!!
> Python allows the dynamic creation of attributes for an instance of an
> object.


See previous discussion in this group "Re: Why don't have an object()
instance a __dict__ attribute by default?" (2007-01-30)

http://groups.google.fr/group/comp.lang.python/browse_thread/thread/a7e952d01b2eea68/961dec95bc20877e?lnk=st&q=&rnum=1#961dec95bc20877e

A+

Laurent.



More information about the Python-list mailing list