Changing the repr() of a class?

Donald McCarthy paddy3118 at tiscali.co.uk
Thu Apr 18 15:11:26 EDT 2002


I would like to alter the way python displays what a class is. I can do this
for an instance of a class using __repr__ but I don't know how to do this
for a class.

Example:
>>> class C(object):
...  def __repr__(self): return 'repr C'
...  def __str__(self): return 'str C'
...
>>> C
<class '__main__.C'>
>>> repr(C)
"<class '__main__.C'>"
>>> str(C)
"<class '__main__.C'>"
>>> i=C()
>>> i
repr C
>>> repr(i)
'repr C'
>>> str(i)
'str C'
>>>

What I want to do is change what is printed when I just type C so it returns
something more meaningful to me whilst creating my program (and learning
python).

Oh - and I'm using python version 2.2

Thanks, Paddy.






More information about the Python-list mailing list