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

Claudio Grondi claudio.grondi at freenet.de
Fri Jan 20 19:12:28 EST 2006


Steven D'Aprano wrote:
> On Thu, 19 Jan 2006 10:08:38 +0100, Claudio Grondi wrote:
> 
> 
>>The point is to find a way to create in Python two indentifiers a and b 
>>without manipulating any of the __eq__ and to __eq__ related functions 
>>in a way, that the simple
>>   if a==b: print 'a==b'
>>statement results in an endless loop.
> 
> 
> Why would you want to?
> 
> 
>>To my knowledge this is not possible to achieve in C, but is probably 
>>achievable in Python.
> 
> 
> I doubt it.
> 
> The closest I can think of is something like this:
> 
> 
> a = []
> a.append(a)
> b = []
> b.append(b)
> a == b
> 
> which I'm told used to cause an endless loop in early versions of Python,
> but now simply returns True.

Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on 
win32 - IDLE 1.1.2
 >>> a=[]
 >>> a.append(a)
 >>> b=[]
 >>> b.append(b)
 >>> a==b

Traceback (most recent call last):
   File "<pyshell#4>", line 1, in -toplevel-
     a==b
RuntimeError: maximum recursion depth exceeded in cmp
 >>>

Claudio



More information about the Python-list mailing list