Unexpected Python Behavior

Simon Wittber simonwittber at gmail.com
Fri Sep 24 09:55:33 CEST 2004

For the first time, I have been bitten by Python. The below code
produces the results:

when I initially expected the results:

It took me a while to work out that default keyword argument values
are likely only evaluated once, which caused the empty dict to be
shared across classes...

It certainly something newbie python coders should look out for!

Simon W.


class X(object):
    def __init__(self, d={}):
        self.d = d

a = X()
b = X()

print a is b
print a.d is b.d


More information about the Python-list mailing list