Default printing behavior for classes

Curtis Yanko cmyanko at bigfoot.com
Thu Jun 24 13:35:32 EDT 1999


In article <7ktoe5$bbl$1 at nnrp1.deja.com>,
  Ben Glazer <glazer at scicomp.com> wrote:
> I'm looking for a way to set default printing behavior for a class
I've
> defined.  Lists are printed in a certain format by default.  I'd like
to
> be able to define this for any type of class.  It'd be really nice if
> there were something I could include in my class definition, e.g.
>
> 	class Board:
> 	    def __init__(self, width=2, height=4, length=20):
> 		self.width = width
> 		self.height = height
> 		self.length = length
>
> 	    def __print__:
> 		print "[%s,%s,%s]" % (width,height,length)
>
> such that
>
> 	b = Board(8,8,2)
> 	print b
>
> would print "[8,8,2]".
>
> Is there anything in Python that encapsulates this functionality, or
> should I resign this desire to a feature req for Python 2.0?
>
> Thanks,
> Ben
>
> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.
>

Isn't that what __repr__ is for?
--
-Curtis Yanko


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




More information about the Python-list mailing list