On Wed, Aug 25, 2010 at 2:23 PM, Glenn Hutchings <zond...@gmail.com> wrote:
> On 25 Aug, 22:18, Ross Williamson <rosswilliamson....@gmail.com> > wrote: > > 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 > > Yes. Just define the __str__ method, like this: > > class foo_class(): > def __str__(self): > return "hello" > -- > http://mail.python.org/mailman/listinfo/python-list > I'd recommend looking at both the __str__ and __repr__ functions at http://docs.python.org/reference/datamodel.html. Depending on your specific use case, its possible __repr__ may be perfered for you.
-- http://mail.python.org/mailman/listinfo/python-list