Default printing behavior for classes
Ben Glazer
glazer at scicomp.com
Thu Jun 24 13:04:14 EDT 1999
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.
More information about the Python-list
mailing list