Overload print
D'Arcy J.M. Cain
darcy at druid.net
Wed Aug 25 17:39:58 EDT 2010
On Wed, 25 Aug 2010 16:18:15 -0500
Ross Williamson <rosswilliamson.spt at gmail.com> wrote:
> Hi All
>
> Is there anyway in a class to overload the print function?
Your terminology threw me off for a moment. You don't want to override
print. You want to override the default representation of an object.
>
> >> class foo_class():
> >> pass
>
> >> cc = foo_class()
> >> print cc
>
> Gives:
>
> <__main__.foo_class instance at ....>
That's the default representation.
> Can I do something like:
>
> >> class foo_class():
> >> def __print__(self):
> >> print "hello"
Close. Check this.
>>> class foo_class():
... def __repr__(self):
... return "hello"
...
>>> x = foo_class()
>>> x
hello
--
D'Arcy J.M. Cain <darcy at druid.net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
More information about the Python-list
mailing list