functions, classes, bound, unbound?

Steven D'Aprano steve at
Sun Mar 25 06:04:36 CEST 2007

On Sat, 24 Mar 2007 20:24:36 -0700, 7stud wrote:

> 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)

Is there a question hidden there somewhere, or are you just reporting on a
fact you have learned and are excited about?

Instance methods always need a self parameter, even if you call them from
the class. That's why you have to provide the instance by hand if you call
them from the class.

If you are trying to create a method that doesn't take a self argument,
have a look at the staticmethod function. There's also a classmethod
function as well.


More information about the Python-list mailing list