Can a simple a==b 'hang' in and endless loop?
claudio.grondi at freenet.de
Wed Jan 18 16:06:37 CET 2006
Steve Holden wrote:
> Claudio Grondi wrote:
>> 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
>> 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.
> Perhaps you could try again in English? :-) Sorry, that's a very complex
> sentence and it isn't clear what yo mean.
Here in English ;-) :
... many other statements here ...
a is b # False
a == b # True
a is b # unpredictable(?)
a == b # True
should in case of code above _always_ give:
a xx b # False
a xx b # True
what becomes only possible if the object holding the integer value 1 is
created only once when a= and reused when executing b=.
> In C, of course, a == b requires that a and b be of compatible types and
> that they have the same value. This means that if they are pointers they
> must point to the same thing (which is exactly what "is" tests for).
>> 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?
> No more than there is in C or, presumably, Java.
> If you want to test for identity, use "is". If you want to test for
> equality, use "==". Of you want to test for something else, kindly
> explain what you want to test for.
More information about the Python-list