Overload print

genxtech jrmy.lnrd at gmail.com
Sat Aug 28 02:03:09 CEST 2010


On Aug 25, 5:18 pm, Ross Williamson <rosswilliamson.... 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
>
> Cheers
>
> Ross
>
> --
> Ross Williamson
> University of Chicago
> Department of Astronomy & Astrophysics
> 773-834-9785 (office)
> 312-504-3051 (Cell)

Are you talking about overriding print(), kind of like overloading the
<< operator in c++ so that you can determine how the foo_class gets
printed?



More information about the Python-list mailing list