python 2.2 question...

Markus Schaber markus at schabi.de
Mon Sep 24 13:16:00 EDT 2001


Paolo Invernizzi <paoloinvernizzi at dmsware.com> schrub:

> I've wondering if there is a solution in 2.2 for this question... (or
> in 2.1 ;)
> 
> I want a custom str rapresentation for a class object... something
> like
> 
>>>>class A(object): pass
>>>>str(A)
> <MyStrRapresentation of class A>

try setting A.__repr__ to a method or function doing whatever you want.

>>> A.__repr__ = lambda: "hallo"            
>>> print A
hallo
>>> str(A)
'hallo'

Works with python 2.0.1 at my machine.

markus
-- 
"The strength of the Constitution lies entirely in the determination of 
each citizen to defend it. Only if every single citizen feels duty 
bound to do his share in this defense are the constitutional rights 
secure." -- Albert Einstein



More information about the Python-list mailing list