How to change default behaviour of a class?

Oleg Broytmann phd at emerald.netskate.ru
Mon Aug 30 06:40:33 EDT 1999


On Mon, 30 Aug 1999, 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.

   "print" uses str() to represent an object. You can override str()
representation by providing __str__ or __repr__ methods in your calss. If
you provide both, you can define different representations for str() and
repr() functions. If you provide obly one, it is good idea to set other:

   __repr__ = __str__ # if you define __str__

> -- 
> bye,
>    Gerhard
> email: sparhawk at eunet.at
>        g.gruber at xsoft.co.at

Oleg.
---- 
    Oleg Broytmann        Netskate/Inter.Net.Ru        phd at emerald.netskate.ru
           Programmers don't die, they just GOSUB without RETURN.





More information about the Python-list mailing list