[Tutor] unbound method function()
Luke Paireepinart
rabidpoobear at gmail.com
Thu Jul 13 05:05:29 CEST 2006
I noticed no one has answered your question yet, so here's my answer, and
someone should come along and correct me if I give you any wrong info.
ryan luna wrote:
> I have two Classes, I have a method in the first class (using
> Pygame/livewires moduls)
>
> Class1(games.Sprite):
> function1(self):
because of the 'self' here, 'function1' expects you to have an instance
of the class 'Class1' already.
example:
>>> classinstance = Class1() #i think you'll need an init function in
Class1, not sure
>>> Class1.function1(classinstance) #this is how you would call it
>>> classinstance.function1() #this is shorthand for the above.
>
> That is being called by the second class
>
> Class2(games.Sprite):
> function2(self):
> Class1.function1()
Here you're trying to call 'function1' which takes a 'self' argument,
but you're not giving it one.
>
> When the code runs i get this error
>
> "unbound method function() must be called with Class2 instance as
> first argument
> (got nothing instead)"
'unbound method' means 'needs class instance to call but didn't get
called with one.'
>
> Its weird (to me) b/c i have other methods calling methods from other
> classes just fine, but on this one method i get that error.
You're probably calling methods defined without a 'self' argument, or
you're using an instance of the class (not the original class name)
without noticing.
HTH,
-Luke
More information about the Tutor
mailing list