Can a simple a==b 'hang' in and endless loop?
Claudio Grondi
claudio.grondi at freenet.de
Thu Jan 19 03:26:25 EST 2006
Fredrik Lundh wrote:
> Dave Hansen wrote:
>
>
>>>Fuzzyman wrote:
>>>
>>>>I'm not familiar with the C basic datatypes - I assume it has an array
>>>>or list like object.
>>>>
>>>>Would it contain a sequence of poitners to the members ? In which case
>>>>they would only be equal if the pointers are the same.
>>>>
>>>>In this case :
>>>>
>>>>a = ['some string']
>>>>b = ['somestring']
>>>>a == b
>>>>False (probably)
>>>>
>>>>Incorrectly using Python syntax for a C example of course :-)
>>>>
>>>
>>>That depends, the C syntax is like this :
>>>
>>>char *a="hello";
>>>char *b="hello";
>>>
>>>assert(a==b);
>>>
>>>// true, the compiler knows the two hello are the same and assign the
>>>same address(sort of id() in python) to a and b
>>
>>No. The C standard says the compiler is _allowed_ to re-use character
>>literal constants, but is not _required_ to do so.
>
>
> I could have sworn that fuzzyman's example contained a literal string in
> an array, and an array comparision, so why are you talking about com-
> paring string literals ? a compiler for which
>
> char* a[] = { "some string" };
> char* b[] = { "some string" };
>
> ...
>
> if (a == b)
> printf("True\n");
>
> prints True is definitely broken.
>
> </F>
Exactly this is what Python does under the hood when writing
a = "some string"
b = "some string"
where a and b are actually, in terms of C, pointer to Python object data
structures which provide strings as arrays where it is possible to say
a[0], but ... if here
if(a==b):
print "True"
_does not_ print True, the Python engine is definitely broken.
Claudio
More information about the Python-list
mailing list