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

Alexander Blinne news at blinne.net
Mon Apr 23 09:34:07 EDT 2012


Am 21.04.2012 14:51, schrieb gst:
> 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 ??

It is simply not defined if, after creation and destruction of an empty
list with some id, a newly created empty list should carry the same id
or not. In cases 1,2 and 4 it happens, but in case 3 it doesn't. This is
simply an implementation detail and neither behaviour is right or wrong.

Alex



More information about the Python-list mailing list