Re: [Tutor] Discrepancy

Magnus Lycka magnus at thinkware.se
Mon Mar 22 19:22:53 EST 2004


Kiran wrote:
> what is the problem
> 
> > x =1
> >x   is   1
> 1
> >
> > (1 is 1)
> 1
> >
> >x is (1 is 1)
> 0
> 
> once i set x=1
> x is 1   returns true(1)
> (1 is 1) returns true(1)
> 
> whereas
>  x is (1 is 1)
> returns 0

Good question!

First of all, you need to understand the difference
between the 'is' operator, and the '==' operator.

You could also note that:

>>> 1 == 1.0
True
>>> 1 is 1.0
False

>>> a = []
>>> b = []
>>> a == b
True
>>> a is b
False

(This is in Python 2.3.2, where the logical True/False
values have been more clearly distinguished from 1 and 0.)

The equality operator (==) returns True (1 in older versions
of Python) if both operands have the same value. Sometimes
this means that type casting takes place, as you can see above.
1 and 1.0 are coerced into the same type and then the values
are compared.

The identity operator (is) returns True if both operands
point to the same object. The distinction between equality
and identity is particularly interesting for mutable objects,
such as lists. Continuing with the lists a and b above...

>>> c = a
>>> c is a
True
>>> a.append(1)
>>> a,b,c
([1], [], [1])

You see? Both a and b were initially empty lists. a == b
evaluated to True. But since the assignment "c = a" makes
c refer to the *same* list object as a, not just any
empty list object. Thus a.append(1) is the same thing as
c.append(1), and a==c will always continue to evaluate to
true as long as you don't reassign or delete a or c.

You can find out the memory address of an object using
the builtin id() function, so "x is y" is actually the
same as "id(x) == id(y)".

I don't have Python 2.2 handy, but it seems there has
been a distinction between the numerical integer value
1 and the boolean truth value True even when True was
spelled "1"... I'm pretty sure this is intentional. There
is certainly a semantic difference between 5 - 4 and
5 > 4.

You can verify this by checking that id(1) is different
then id(1==1).

Note again that "is" is not the operator to use to verify
that two values are equal. Its intention is to show that
two variables (names) both refer to the same object in the
computer's memory.

It's really just a technicality that "1 is 1" evaluated
to True all the time: Python interns some non-mutable
objects, such as small integers and strings, i.e. if you 
use the same value two times in your code, Python will 
notice that this value is already defined, and reuse it,
instead of wasting space with another, identical, object.

(It's up to programmers to reuse code, but Python reuses
data for us! :)

Naturally, Python can't to this with mutable objects, such
as lists, but for immutable objects such as strings and
numerical values, it can be an efficient way to work. Don't
rely on this to happen though. It's not something which is
required of Python, it's just an implementation detail to
improve performance. It might be gone in the next version.

It's not even consistent over all values of a particular
type in a given version of Python.

>>> s1 = "Hello"
>>> s2 = "Hello"
>>> s1 is s2
True
>>> s1 = "Hello"*999
>>> s2 = "Hello"*999
>>> s1 is s2
False
>>> a = 123456789
>>> type(a)
<type 'int'>
>>> b = 123456789
>>> a is b
False

Even the same value might get different treatment!

>>> 1234567890123456 is 1234567890123456
True
>>> x = 1234567890123456
>>> y = 1234567890123456
>>> id(x)
9282744
>>> id(y)
9282672
>>> x is y
False

In conclusion, you should typically use '==' to
compare things for equality. You might want to use
'is' to see that two names actually refer to the 
same object, but most of the time you know if they 
do. There is only one object of the NoneType, so
it's completely correct to write "x is None", but
"x is 5" is an error waiting to happen in a future
Python version...



-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus at thinkware.se



More information about the Tutor mailing list