Unexpected Python Behavior

Simon Wittber simonwittber at gmail.com
Fri Sep 24 03:55:33 EDT 2004


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

when I initially expected the results:
False
False

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.

---snip-here---

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

a = X()
b = X()

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

---snip-here---



More information about the Python-list mailing list