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

Fuzzyman fuzzyman at gmail.com
Wed Jan 18 08:38:45 EST 2006


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




More information about the Python-list mailing list