Basic Class/Instance Question

Wildemar Wildenburger wildemar at freakmail.de
Wed May 23 08:52:36 EDT 2007


Antoon Pardon wrote:
>> This is a FAQ. Default arguments are only evaled once - when the def 
>> statement is evaled (which is usually at import time). The solution is 
>> simple: don't use mutable objects as default arguments:
>>     
>
> An immutable object would have given the same behaviour in this case
>
> class A(object):
>     def __init__(self, val = ()):
>         self.val=val
>
> obj1 = A()
> obj2 = A()
>
> print obj1 is obj2               # False
> print obj1.val is obj2.val       # True
>
>   
Yeah, but is that a problem? Since you can not change them anyway, 
there's no harm.
W




More information about the Python-list mailing list