why () is () and [] is [] work in other way?

gst g.starck at gmail.com
Sat Apr 21 08:51:50 EDT 2012


Le samedi 21 avril 2012 10:46:39 UTC+2, Alexander Blinne a écrit :
> Am 21.04.2012 05:25, schrieb Rotwang:
> 
> This happens only because the first [] gets destroyed after evaluation
> of id([]). The second [] then by accident gets the same id as the first
> one had.
> 
> >>> a = []
> >>> b = []
> >>> id(a) == id(b)
> False
> 
> Greetings

Hi,

I played (python3.2) a bit on that and :

case 1) Ok to me (right hand side is a tuple, whose elements are evaluated one per one and have same effect as your explanation (first [] is destroyed right before the second one is created) :

>>> x, y = id([]), id([])
>>> x == y
True
>>> 


case 2) also ok to me:

>>> x = id([]) ; y = id([])
>>> x == y
True
>>> 


case 3) NOT ok to me :

>>> x = id([])
>>> y = id([])
>>> x == y
False
>>> 


case 4) ok to me :

>>> def f():
	x = id([])
	y = id([])
	return x == y

>>> f()
True
>>> 


I'd have thought that cases 2&3 are totally, while 3&4 nearly, syntactically equal and that case 3 is the incorrect result..

how would you explain case 3 vs cases 2 and 4 ??


regards,

gs.




More information about the Python-list mailing list