New-style classes and special methods

Terry Reedy tjreedy at udel.edu
Mon Feb 24 17:07:21 CET 2003


"Alex Martelli" <aleax at aleax.it> wrote in message
news:qhl6a.303192$AA2.11531457 at news2.tin.it...
> Of course, this class is nearly unusable...:
>
> >>> f
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: <lambda>() takes exactly 1 argument (0 given)

Yes, subverting Python's 'Every object has a printable representation'
feature can be a real PITA:

>>> globals()
{'C': <class __main__.C at 0x007960D0>, 'f': Traceback (most recent
call last):
  File "<stdin>", line 1, in ?
TypeError: <lambda>() takes exactly 1 argument (0 given)

I discovered this time bomb when I came back to my interactive
session, an hour after running the experiment with the cracked class,
to do something completely different.

I am tempted to suggest that the feature be made more robust by
wrapping internal calls to __repr__/__str__ with try/except.  The
except clause would call the dependable default versions -- and maybe
tack on a bug note.

< C instance at 0xabc with bad __str__>

TJR






More information about the Python-list mailing list