Reference gotcha

anton muhin antonmuhin at rambler.ru
Mon Feb 9 11:42:36 EST 2004


Ed Schofield wrote:

> 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
> 
> 

Standard trick is to use single element list or just a small object with 
integer field.

hth,
anton.



More information about the Python-list mailing list