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

Claudio Grondi claudio.grondi at freenet.de
Wed Jan 18 08:59:08 EST 2006


Fuzzyman wrote:
> Claudio Grondi wrote:
> [snip..]
> 
>>Thanks for the quick reply.
>>
>>I see, that I have overseen, that as Fredrik also stated, one can
>>directly manipulate __eq__() as the easiest way to achieve what I
>>requested.
>>
>>To explain why I am not happy with it, I will try here to give some more
>>background information. Sorry for not doing it directly, but as already
>>stated I have forgot about the possibility to use __eq__().
>>
>>In Python the built in '==' operator (when not manipulated in own code)
>>behaves not as the '==' operator e.g. in C or Javascript, because it
>>iterates over arrays (i.e. lists) doing many comparisons instead of
>>comparing only two 'values'. Coming from C or Javascript one would
>>expect '==' to compare the 'pointers' to the arrays and not to iterate
>>over all elements of the lists.
> 
> 
> In Python the equality operator ('==') compares values. For sequence
> and mapping type objects this can be a (relatively) expensive
> operation.
> 
> You are probably looking for the identity operator which just
> (effectively) compares pointers ('is').
> 
>    a is b
> 
> does more what you would expect.
> 
> This is a better system than the ones you describe. :-)
> 
> All the best,
> 
> Fuzzyman
> http://www.voidspace.org.uk/python/index.shtml
> 
> 
>>With the solution to the question above I intended to have an example of
>>Python code which outcome is an endless loop and the problem causing it
>>very hard to find if one thinks in terms of C or Javascript when
>>considering lists (as arrays) and the function of '==' operator.
>>
>>Claudio
> 
> 
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 
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?

Claudio



More information about the Python-list mailing list