Can anyone explain the behaviour of python when running this script? >>> def method(n, bits=[]): ... bits.append(n) ... print bits ... >>> method(1) [1] >>> method(2) [1, 2]
It's the same in python 1.5, 2.3 and 2.4 so it's not a bug. But I expected the variable "bits" to be re-initialised to an empty list as each method was called. Whenever I explain optional keyword arguments to someone I have usually (wrongly as it turns out) said it is equivalent to: >>> def method(n, bits=None): ... if bits is None: ... bits=[] ... bits.append(n) ... print bits ... >>> method(1) [1] >>> method(2) [2] Is there a good reason why these scripts are not the same? I can understand how/why they are different, it's just not what I expected. (It seems strange to me that the result of the first method can only be determined if you know how many times it has already been called) Is this behaviour what you would (should?) intuitively expect? Thanks, Brian -- http://mail.python.org/mailman/listinfo/python-list