Reference gotcha
Ed Schofield
schofield at ftw.at
Mon Feb 9 10:46:41 EST 2004
Hi all,
This interested me:
>>> a1 = a2 = {}
>>> a1['blah'] = 1
>>> a2
{'blah': 1}
>>> a1 == a2
True
Whereas ...
>>> b1 = b2 = 0
>>> b1 += 1
>>> b2
0
>>> b1 == b2
False
So how should one create a robust alias for a numeric type?
This bit me when I was trying to do the following in a class:
def __init__(self, data):
a = self.a = {}
b = self.b = {}
c = self.c = 0
# and increment them here ...
Best regards,
Ed Schofield
More information about the Python-list
mailing list