functions, classes, bound, unbound?

7stud bbxx789_05ss at yahoo.com
Sat Mar 24 23:24:36 EDT 2007


Here is some example code that produces an error:

class Test(object):
        def greet():
                print "Hello"

t = Test()
t.greet()
TypeError: greet() takes no arguments (1 given)

Ok.  That makes sense.  t.greet() is a "bound method", so something
automatically relays the instance object to greet(), and since greet()
is defined with no parameters-->Error.

Hmmm...I know from testing several examples that if I call a function
that's defined inside a class, and I use the class name preceding the
function call, then I have to send the instance manually.  So, based
on that error message, I'll just call the method using the class name
and not send the instance object:

Test.greet()

TypeError: unbound method greet() must be called with Test instance as
first argument (got nothing instead)




More information about the Python-list mailing list