[Tutor] Function output

Wesley J. Chun wesc@alpha.ece.ucsb.edu
Wed, 28 Jun 2000 03:16:46 -0700 (PDT)


    > Date: Mon, 26 Jun 2000 22:51:42 -0500
    > From: "William Perry" <wmperry@903internet.com>
    > 
    > >>> class rollum:
    >  def funct1 (self):
    >   s = " Hello World"
    >   return s
    >  def output (self):
    >   a = funct1()
    >   return a
    >  
    > >>> rollum.output()
    > Traceback (innermost last):
    >   File "<pyshell#42>", line 1, in ?
    >     rollum.output()
    > TypeError: unbound method must be called with class instance 1st argument
    > 
    > >>> class rollum:
    >  def funct1 (self):
    >   s = " Hello World"
    >   return s
    >  def output (self):
    >   a = funct1()
    >   return a
    >  def printer (self):
    >   print self.output()
    >   
    > >>> rollum.printer()
    > Traceback (innermost last):
    >   File "<pyshell#47>", line 1, in ?
    >     rollum.printer()
    > TypeError: unbound method must be called with class instance 1st argument

hi...

oh so close.  the key to each problem you run into is due
to the fact that the assignment you have in output() does
not called with an instance.  so change...

    >   a = funct1()

to

    >   a = self.funct1()

... just like how you called self.output() from the printer
method.  then things should work fine.

as you can probably tell when you call self.foo(), what's
really happening is class.foo(self)... this is handled by
the interpreter so you don't have to worry about (although
you can make the latter call too)!  if you wanted to try it,
you would enter:

rollum.funct1(self) # rather than self.funct1()

in either case, an instance is passed in to appease the
interpreter.

hope this helps!!

-wesley

"Core Python Programming", Prentice-Hall, TBP Summer 2000
    http://www.phptr.com/ptrbooks/ptr_0130260363.html
    http://www.softpro.com/languages-python.html

wesley.j.chun :: wesc@alpha.ece.ucsb.edu
cyberweb.consulting :: silicon.valley, ca
http://www.roadkill.com/~wesc/cyberweb/