python 2.2 question...

Paolo Invernizzi paoloinvernizzi at dmsware.com
Tue Sep 25 03:54:44 EDT 2001


Markus Wrote...
-------------
> try setting A.__repr__ to a method or function doing whatever you want.
>
> >>> A.__repr__ = lambda: "hallo"
> >>> print A
> hallo
> >>> str(A)
> 'hallo'
-------------

Things work differently in 2.2, and not only for "object" derived classes...
(for now ;)

Python 2.2a3 (#23, Sep  7 2001, 01:43:22) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help

>>> class A: pass
>>> A.__repr__= lambda: "Repr of A"
>>> print A
__main__.A
>>> str(A)
'__main__.A'
>>> repr(A)
'<class __main__.A at 0x009F8D2C>'

>>> class A(object):  pass
>>> A.__repr__= lambda: "Repr of A"
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in ?
    A.__repr__= lambda: "Repr of A"
TypeError: can't set type attributes

Paolo Invernizzi








More information about the Python-list mailing list