Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: TypeError: unbound method

23 views
Skip to first unread message

Chris Rebert

unread,
Jul 16, 2009, 9:08:45 PM7/16/09
to Ronn Ross, pytho...@python.org
On Thu, Jul 16, 2009 at 5:57 PM, Ronn Ross<ronn...@gmail.com> wrote:
> Hello all,
>
> Created a python file that has a class and three methods. When I jump into
> interactive mode I import like so:
>
> from <file> import <class>
>
> Now when I try to use a method from the class:
>
> var1 = class.method()
>
> It give me this error:
> TypeError: unbound method <methodname> must be called with <classname>
> instance as first argument (got int instance instead)
>
> I'm not sure what is going on event after consulting with google

You're trying to call an instance method on the class itself, which
doesn't make sense.
You need to first create an instance of the class to invoke the method on.
i.e.:

instance = TheClass(constuctor_arguments_here)
var1 = instance.method()

Cheers,
Chris
--
http://blog.rebertia.com

Steven D'Aprano

unread,
Jul 17, 2009, 3:56:07 AM7/17/09
to
On Thu, 16 Jul 2009 18:08:45 -0700, Chris Rebert wrote:

> You're trying to call an instance method on the class itself, which
> doesn't make sense.

Oh I don't know, people do it all the time -- just not the way the OP
did :)

> You need to first create an instance of the class to invoke the method
> on. i.e.:
>
> instance = TheClass(constuctor_arguments_here)
> var1 = instance.method()

Or explicitly pass an instance to the method when calling from the class:

TheClass.method(TheClass(constuctor_arguments_here))


Which strangely enough, is useful at times.

class K(parent):
def method(self, args):
x = parent.method(self, args)
return x + 1

--
Steven

0 new messages