[Tutor] Unbound Method Error

Greg Nielsen gollumgreg407366 at gmail.com
Mon Apr 25 11:36:33 CEST 2011


First and foremost, thank you Steven for your quick and well written
response. It means a lot to me that you took the time out of your day to
answer my question, and it has really helped me better understand what it
going on.

So the full trace back for my error is as follows:

Traceback (most recent call last):
  File "C:\Users\Greg\Documents\NetBeansProjects\Red Racer
26\src\redracer26.py", line 19, in <module>
    main()
  File "C:\Users\Greg\Documents\NetBeansProjects\Red Racer
26\src\redracer26.py", line 16, in main
    redracerstates26.game(startLives)
  File "C:\Users\Greg\Documents\NetBeansProjects\Red Racer
26\src\redracerstates26.py", line 87, in game
    redracersprites26.BlueCarSprite.collide(BlueCarSprite, playerCar)
TypeError: unbound method collide() must be called with BlueCarSprite
instance as first argument (got GreenCarSprite instance instead)
After reading through your explanation of unbound methods (and wrapping my
head around the concept), I came up with a quick fix that lead to a new
issue in my code. Here is what I think should work:

crash = pygame.sprite.spritecollide(playerCar, computerSprites, False)
        if crash:
            for BlueCarSprite in crash:
                redracersprites26.BlueCarSprite.collide(crash, playerCar)
            for GreenCarSprite in crash:
                redracersprites26.GreenCarSprite.collide(crash, playerCar)
            for TruckSprite in crash:
                redracersprites26.TruckSprite.collide(crash, playerCar)

However, the spritecollide method returns a list, which my collide method
isn't expecting. The list should only contain the name of the sprite the
player hit. In Python 3, the method would just pull the variable out of the
list and work with it, but in Python 2, it's not expecting a list, causing
an error similar to the one seen previously:

Traceback (most recent call last):
  File "C:\Users\Greg\Documents\NetBeansProjects\Red Racer
26\src\redracer26.py", line 19, in <module>
    main()
  File "C:\Users\Greg\Documents\NetBeansProjects\Red Racer
26\src\redracer26.py", line 16, in main
    redracerstates26.game(startLives)
  File "C:\Users\Greg\Documents\NetBeansProjects\Red Racer
26\src\redracerstates26.py", line 87, in game
    redracersprites26.BlueCarSprite.collide(crash, playerCar)
TypeError: unbound method collide() must be called with BlueCarSprite
instance as first argument (got list instance instead)
So what really has to happen now (I think) is that the list needs to be
unpacked into just a simple variable containing the name of the sprite in
question. This way the method receives the name of the instance (self) in
the way it was expecting to receive it. In your opinion, should I unpacked
the list before passing the data into the method, or attempt to define
"self" as a list? Is the latter even possible? I would think it would have
to be because otherwise Python could pick a variable type which you don't
want or aren't expecting it to pick and need to force it to take a certain
type of input.

And thank you for pointing out the fact I could just make a CarSprite class
and have subclasses (basically just a different sprite image) work with the
details. You just removed over 150 lines of redundant code from my program.
And once again, thank you Steven for taking the time to help me better
understand the whole unbound method concept.

Greg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110425/0c51b0ae/attachment.html>


More information about the Tutor mailing list