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

Steve Holden steve at holdenweb.com
Wed Jan 18 13:53:39 CET 2006


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
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list