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

Claudio Grondi claudio.grondi at freenet.de
Wed Jan 18 09:29:24 EST 2006


Steve Holden wrote:
> Claudio Grondi wrote:
> 
>> Steve Holden wrote:
>>
>>> Claudio Grondi wrote:
>>>
>>>
>>>> In the process of learning about some deeper details of Python I am 
>>>> curious if it is possible to write a 'prefix' code assigning to a 
>>>> and b something special, so, that Python gets trapped in an endless 
>>>> loop in a line with:
>>>>
>>>> if a==b: print 'OK'
>>>>
>>>> I mean, it would be of much help to me on my way to understanding 
>>>> Python to know how such prefix code leading to an endless loop can 
>>>> look like and if it is eventually not possible to write such code, 
>>>> to know why it is not possible?
>>>>
>>>> My own first rough idea was to create generators which never end and 
>>>> use them in the '==' comparison, but I have not well understood how 
>>>> to write and use generators yet, so I expect from studying this 
>>>> special case to come to some enlightenment.
>>>>
>>>
>>> Well, you could try this:
>>>
>>> >>> class thing:
>>> ...   def __eq__(self, other):
>>> ...      return other == self
>>> ...
>>> >>> a = thing()
>>> >>> b = thing()
>>> >>> a == b
>>> Traceback (most recent call last):
>>>  File "<stdin>", line 1, in ?
>>>  File "<stdin>", line 3, in __eq__
>>>  File "<stdin>", line 3, in __eq__
>>>  File "<stdin>", line 3, in __eq__
>>>    ...
>>>  File "<stdin>", line 3, in __eq__
>>>  File "<stdin>", line 3, in __eq__
>>> RuntimeError: maximum recursion depth exceeded
>>> >>>
>>>
>>> Was that what you meant? Or something more like:
>>>
>>> >>> class thing:
>>> ...   def __eq__(self, other):
>>> ...     import time; time.sleep(1000000)
>>> ...
>>> >>> a = thing()
>>> >>> b = thing()
>>> >>> a == b
>>>
>>> regards
>>> Steve
>>
>>
>> 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.
>> 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.
>>
> If your assertiona about C and Java are correct you would, of course, 
> describing a deficiency of those languages, where a variable refers to a 
> reserved area of storage intended to hold a value of a specific type (or 
> a specific uinion of types).
> 
> To the Python user C and Java appear to be confusing "equality" with 
> "identity". The == operator in C, certainly, compares identity (whether 
> of values or of pointers to structured values). Frankly I don't choose 
> to remember enough Java to determine the correctness of your assertion 
> in that language.
> 
> In Python a name is intended to be bound as a reference to an object of 
> any type whatsoever (the type pf the object is stored as a part of the 
> value). Equality is generally defined as "has the same value", hence the 
> ability to define it specifically for user-defined types.
> 
> In Python you test for "is the same object" with the "is" operator. As in
> 
>  >>> a = {1:2, 3:4}
>  >>> b = {1:2, 3:4}
>  >>> a == b
> True
>  >>> a is b
> False
>  >>>
> 
> regards
>  Steve

The problem here is, that I mean, that in Python it makes no sense to 
talk about a value of an object, because it leads to weird things when 
trying to give a definition what a value of an object is.

It seems, that in Python there is a lack of operator able to compare 
values as it is the case in C and Javascript, simply because in Python 
there are no really such things as values, so there is no need to 
compare them.

The higher level of abstraction/indirection in Python results in making 
the concepts of 'value', 'having a value' or 'comparing values' useless, 
where it helps in C to express the difference between address and 
content at that address and to distinguish between the information 
telling _what_ is stored in memory and the information about _where_ it 
is stored.

It appears to me, that the whole subject of what an identifier in Python 
represents and if there is such thingy as 'value' in Python, is not 
perfectly well understood even by Python experts programming also in 
many other languages and therefore the vital differences in concepts 
behind Python and e.g. C/C++ is only poorly documented . What else would 
be the reason for the over-long discussion in the "Is 'everything' a 
refrence or isn't it?" thread in this newsgroup?

 From what I know about Python up to now, the problem with the concept 
of 'value' in Python is, that it depends ... and it depends on so many 
things, that it is hard to give any general valid statement about it 
without writing an entire chapter full of details and special cases.

Claudio



More information about the Python-list mailing list