Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

TypeError: unbound method

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


ronn.ross at gmail

Jul 16, 2009, 5:57 PM

Post #1 of 3 (250 views)
Permalink
TypeError: unbound method

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


clp2 at rebertia

Jul 16, 2009, 6:08 PM

Post #2 of 3 (233 views)
Permalink
Re: TypeError: unbound method [In reply to]

On Thu, Jul 16, 2009 at 5:57 PM, Ronn Ross<ronn.ross [at] gmail> 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
--
http://mail.python.org/mailman/listinfo/python-list


steve at REMOVE-THIS-cybersource

Jul 17, 2009, 12:56 AM

Post #3 of 3 (237 views)
Permalink
Re: TypeError: unbound method [In reply 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
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.