Abend with cls.__repr__ = cls.__str__ on Windows.

J Peyret jpeyret at gmail.com
Thu Mar 17 20:24:36 EDT 2011


This gives a particularly nasty abend in Windows - "Python.exe has
stopped working", rather than a regular exception stack error.  I've
fixed it, after I figured out the cause, which took a while, but maybe
someone will benefit from this.

Python 2.6.5 on Windows 7.



class Foo(object):
    pass

    #def __str__(self):  #if you have this defined, no abend
    #    return "a Foo"

Foo.__repr__ = Foo.__str__   # this will cause an abend.

#Foo.__str__ = Foo.__repr__  #do this instead, no abend

foo = Foo()

print str(foo)



I suspect that object.__str__ is really object.__repr__ by default, as
they both print out the same string, so that this doesn't make any
sense.

What was I trying to achieve?  Leveraging __str__ to debug instances
in lists and containers.  In that case, __repr__ is called and I
usually do:

class Foo(object):
  def __str__(self):
     return "a foo"
  def __repr__(self):  #was trying to avoid this.
     return str(self)



More information about the Python-list mailing list