Overload print
Chris Rebert
chris at rebertia.com
Wed Aug 25 17:32:25 EDT 2010
On Wed, Aug 25, 2010 at 2:18 PM, Ross Williamson
<rosswilliamson.spt at gmail.com> wrote:
> Hi All
>
> Is there anyway in a class to overload the print function?
>
>>> class foo_class():
>>> pass
>
>>> cc = foo_class()
>>> print cc
>
> Gives:
>
> <__main__.foo_class instance at ....>
>
> Can I do something like:
>
>>> class foo_class():
>>> def __print__(self):
>>> print "hello"
>
>>> cc = foo_class()
>>> print cc
>
> Gives:
>
> hello
>
> I'm looking at finding nice way to print variables in a class just by
> asking to print it
You want to overload the __str__() method:
http://docs.python.org/reference/datamodel.html#object.__str__
Cheers,
Chris
--
http://blog.rebertia.com
More information about the Python-list
mailing list