Re: Overload print

Discussion in 'Python' started by Alexander Kapps, Aug 25, 2010.

  1. 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
    Alexander Kapps, Aug 25, 2010
    #1
    1. Advertising

  2. Alexander Kapps

    John Roth Guest

    On Aug 25, 3:42 pm, Alexander Kapps <> 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
    John Roth, Aug 26, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Piotre Ugrumov
    Replies:
    3
    Views:
    360
    Nick Hounsome
    Jan 25, 2004
  2. keto
    Replies:
    0
    Views:
    884
  3. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    330
    David Cournapeau
    Dec 30, 2008
  4. Ross Williamson

    Overload print

    Ross Williamson, Aug 25, 2010, in forum: Python
    Replies:
    1
    Views:
    262
    genxtech
    Aug 28, 2010
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    229
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page