How to change default behaviour of a class?

Thomas Wouters thomas at xs4all.nl
Mon Aug 30 12:31:57 CEST 1999


On Mon, Aug 30, 1999 at 12:36:00PM +0200, Gerhard W. Gruber wrote:

> How can I change what is printed when I use the statement "print class"?
> Usually this leads to an output like <class instance at 9aad60> but I
> rather want it to print variables contained in my class instead of the
> pointer adress.

define a __repr__() method, that takes one argument (self):

>>> class A:
...     pass
... 
>>> class B:
...     def __repr__(self):
...             return "Cheesecake"
... 
>>> a, b = A(), B()
>>> print a
<__main__.A instance at 809a388>
>>> print b
Cheesecake
>>> 


-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list