[Tutor] How to call a method with a print statement?

Kent Johnson kent37 at tds.net
Thu Nov 12 12:28:40 CET 2009


On Thu, Nov 12, 2009 at 5:31 AM, Modulok <modulok at gmail.com> wrote:
> List,
>
> How do I get a print statement to call a class method? I'm using
> python 2.5. I did it long ago, but can't remember how, or even where I
> learned it from. Something like:
>
> class Foo():
>   def __init__(self):
>      pass
>
>   def  ***part I can't remember goes here***
>      print "hello world!"

You want the __str__() special method. Note that it returns a string,
it doesn't itself print:
def __str__(self):
  return "hello world"

Kent


More information about the Tutor mailing list