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

Fuzzyman fuzzyman at gmail.com
Wed Jan 18 11:40:53 EST 2006


Peter Hansen wrote:
> Claudio Grondi wrote:
> > 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?
>
> I think you fundamentally can't get what you want here.  It would be
> quite possible to implement an optimization on the == operator in Python
> which checked whether two items were identical (i.e. "is", the same as
> comparing their addresses).  This would do just what C is doing in the
> case of comparing two lists which are the same, but then the following
> code could not be written:
> [snip..]

What you could do is define a subclass of list that compared for
identity rather than comparing the contents of the list.

Alternatively you could test the contents by identity.

All the best,

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




More information about the Python-list mailing list