Problems with code

Chris Rebert clp2 at rebertia.com
Mon Mar 30 12:17:02 EDT 2009


2009/3/30 Zach Goscha <ZTank5 at gmail.com>:
> Hi,
>
> I am trying to call an unbound method (Map.Background) but getting the
> following error:
>  TypeError: unbound method background() must be called with Map instance as
> first argument (got nothing instead)
>
> Here is some of the code(not completed)
>
> Thanks in Advance
> - Zach
>
>
> Code:
> class Knight(games.Sprite):
>     """ A moving knight. """
>     SWORD_DELAY = 50
>     sword_wait = 0
>
>
>     def update(self):
>         """ moving knight based on keys pressed. """
<snip>
>         if self.bottom < 0:
>             self.top = games.screen.height
>             Map.background()
<snip>
> class Map(games.Sprite):
>
>     def background(self):
>         new_back = games.load_image("map3.jpg",
> transparent = False)

In the above code, you do `Map.background()`; this is invalid.
background() is an /instance/ method of Map objects, *not* a
classmethod of Map, so you can't call it on the class itself, only
instances of it. In Java-ish terms, you're trying to call a non-static
method like it's a static method.
You either need to make background() a classmethod, or create an
instance of Map to call the method on instead (perhaps you have a
self.map variable and this is just a typo?).

Cheers,
Chris
-- 
I have a blog:
http://blog.rebertia.com



More information about the Python-list mailing list