Can a simple a==b 'hang' in and endless loop?

Fuzzyman fuzzyman at gmail.com
Wed Jan 18 09:59:42 EST 2006


Claudio Grondi wrote:
[snip..]
> Yes, I know about 'is',
>
> but I mean, that it is not possible to use 'is' as replacement for '=='
> operator to achieve in Python same behaviour as it is the case in C and
> Javascript when comparing values with '=='.
> 'is' does the C, Javascript job when comparing lists, but I mean it
> fails to give fully predictable results when applied to elements of
> lists in case there exist duplicate objects with same 'value' i.e. e.g.
> there are two different objects storing the integer value 1, what I mean

Hmmm... :

 >>> a = 1
 >>> b = 1
 >>> a is b
True

Doesn't work with arbitrary longs of course...

> can happen when there is enough other code between the Python code lines
> assigning the integer value 1 to a list element or any other identifier.
> Or is there in Python a 100% reliable mechanism assuring, that there is
> one and _only one_ object carrying a given 'value' (at least for the
> built in types as integer, long integer, string, float) and if this
> value is to be assigned to a list element or any other literal the
> already existing object (if any) will be found and used/referenced?
>

So the Java/C '==' operator sometimes works like '==' in Python and
sometimes works like 'is' ? (It switches between equality and identity
depending on the types being compared IIUC).

My understanding is that you can't use '==' in Java to compare strings,
because it is an identity test not an equality test. You have to use a
string method to compare strings. This bites quite a few people...

Anyway, AFAICT your problem only matters in the abstract (where you are
theoretically comparing objects you haven't specified the type of and
*might* want to use '==' and *might* want to use 'is').

For practical purposes '==' will do what you want, unless you
deliberately create buggy code to cause a problem when you use it...

Unless you can post an example of *non-buggy* code that doesn't behave
how you expect ?

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

> Claudio




More information about the Python-list mailing list