On 2007-05-23, Wildemar Wildenburger <[EMAIL PROTECTED]> wrote: > 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.
I think it can make comprehension harder if you suggest in your explanation that a partcilular behaviour is linked to an object being mutable while that has nothing to do with it. You are correct that one can't mutate the val attribute but some code might depend on the distinction identical or not instead of equal or not. Not understanding this issue of default arguments could cause a bug in such code even if the default argument is not mutable. -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list