Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Overload print

Reply
Thread Tools

Re: Overload print

 
 
Alexander Kapps
Guest
Posts: n/a
 
      08-25-2010
Ross Williamson wrote:
> Hi All
>
> Is there anyway in a class to overload the print function?


In Python <= 2.x "print" is a statement and thus can't be
"overloaded". That's exactly the reason, why Python 3 has turned
"print" into a function.

>>> class foo_class():
>>> def __print__(self):
>>> print "hello"

>
>>> cc = foo_class()
>>> print cc

>
> Gives:
>
> hello


Hmm, on what Python version are you? To my knowledge there is no
__print__ special method. Did you mean __str__ or __repr__ ?

> I'm looking at finding nice way to print variables in a class just by
> asking to print it


In Python3 you *can* overload print(), but still, you better define
__str__() on your class to return a string, representing what ever
you want:

In [11]: class Foo(object):
....: def __str__(self):
....: return "foo"
....:
....:

In [12]: f = Foo()

In [13]: print f
foo

 
Reply With Quote
 
 
 
 
John Roth
Guest
Posts: n/a
 
      08-26-2010
On Aug 25, 3:42*pm, Alexander Kapps <alex.ka...@web.de> wrote:
> Ross Williamson wrote:
> > Hi All

>
> > Is there anyway in a class to overload the print function?

>
> In Python <= 2.x "print" is a statement and thus can't be
> "overloaded". That's exactly the reason, why Python 3 has turned
> "print" into a function.
>
> >>> class foo_class():
> >>> * * def __print__(self):
> >>> * * * * * print "hello"

>
> >>> cc = foo_class()
> >>> print cc

>
> > Gives:

>
> > hello

>
> Hmm, on what Python version are you? To my knowledge there is no
> __print__ special method. Did you mean __str__ or __repr__ ?
>
> > I'm looking at finding nice way to print variables in a class just by
> > asking to print it

>
> In Python3 you *can* overload print(), but still, you better define
> __str__() on your class to return a string, representing what ever
> you want:
>
> In [11]: class Foo(object):
> * * ....: * * def __str__(self):
> * * ....: * * * * return "foo"
> * * ....:
> * * ....:
>
> In [12]: f = Foo()
>
> In [13]: print f
> foo


Maybe what the OP really wants is the format() method on a string?
That gives a very rich set of override options, at the expense of not
using the print statement/method, including the ability to define your
own formatting language for a class.

John Roth
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
print a vs print '%s' % a vs print '%f' a David Cournapeau Python 0 12-30-2008 03:19 AM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
function overload (not operator overload) Ying-Chieh Liao Perl Misc 3 10-11-2004 11:24 AM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM
How use the overload of>> (or<<) of a class in the overload of << and >> of another class? Piotre Ugrumov C++ 3 01-25-2004 08:08 PM



Advertisments