Can a simple a==b 'hang' in and endless loop?
Claudio Grondi
claudio.grondi at freenet.de
Wed Jan 18 10:06:37 EST 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
>> 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.
>
>
> 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 ;-) :
a=[1]
... many other statements here ...
b=[1]
a is b # False
a == b # True
a[0] is b[0] # unpredictable(?)
a[0] == b[0] # True
so a C, Javascript equivalent '==' operator (called here e.g. 'xx')
should in case of code above _always_ give:
a xx b # False
a[0] xx b[0] # True
what becomes only possible if the object holding the integer value 1 is
created only once when a=[1] and reused when executing b=[1].
Claudio
>
> 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.
>
> regards
> Steve
More information about the Python-list
mailing list