[Tutor] __init__, default values and dict's
Bart Cramer
bart.cramer at gmail.com
Thu Aug 14 12:05:50 CEST 2008
Dear tutors,
I have the following code snippet:
class N (object) :
def __init__ (self, d={}) :
self.d = d
n1 = N ()
n1.d['a'] = 1
n2 = N ()
n2.d['a'] = 2
print n1.d['a']
print n2.d['a']
I assumed that on each call of the __init__ without a dictionary
provided, a new dictionary is created. This turns out to be not the
case:
user at ubuntu804desktop:~$ python test.py
2
2
Does anybody know why this is the case? And what would be the most
convenient solution to enforce the dynamic creation of the dictionary?
Many thanks for your time and patience,
Bart.
More information about the Tutor
mailing list