On 20/04/2012 20:10, dmitrey wrote:
I have spent some time searching for a bug in my code, it was due to
different work of "is" with () and []:
() is ()
True
[] is []
False

(Python 2.7.2+ (default, Oct  4 2011, 20:03:08)
[GCC 4.6.1] )

Is this what it should be or maybe yielding unified result is better?
D.

The reason that "[] is []" evaluates to false is because lists are mutable and each expression "[]" evaluates to a different object. Therefore you can change one without changing the other:

>>> a, b = [], []
>>> a.append(None)
>>> a
[None]
>>> b
[]

On the other hand tuples are immutable, so there's no danger in having each literal () evaluate to the same object - since you can't change a tuple but only replace a reference to a tuple with a reference to another tuple, there's no need to have the two "()"s in something like

>>> a, b = (), ()

evaluate to different objects, since there is no danger that one can affect the value of b by doing stuff to a.

I believe it says somewhere in the Python docs that it's undefined and implementation-dependent whether two identical expressions have the same identity when the result of each is immutable, though I can't find the quotation right now. If my recollection is correct then you probably shouldn't rely on something like "() is ()" giving the same answer across platforms.

--
Hate music? Then you'll hate this:

http://tinyurl.com/psymix
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to