Using __repr__ or __str__ for own printable class?

Mads Orbesen Troest mads at troest.NEVERMORE.dk
Mon Apr 14 14:05:42 EDT 2003


On Mon, 14 Apr 2003 17:57:46 +1200, Greg Ewing (using news.cis.dfn.de) 
wrote:

> If you care about the precise format of the string
> representation you're trying to get for a container,
> you shouldn't be using either str() or repr() --
> you should be writing your own function that does
> exactly what you want.

But then one wouldn't be able to do a standard "print" of the class, would 
one? That was what I was after in the first place, and it seemed like the 
way to do it is to override the __str__ method. I would like to be able to 
do this ...
	print someInstanceOfMyClass

... and then determine what should be printed. This works with __str__ but 
I was/am afraid if this overriding of __str__ will conflict with other uses 
of __str__ and/or __repr__.

Regards,
   /\/\\ads Orbesen Troest




More information about the Python-list mailing list