[Tutor] UnboundLocal Error

Alan Gauld alan.gauld at freenet.co.uk
Mon Nov 21 00:33:27 CET 2005


> The comments explain the situation pretty well.

Unfortunately the formatting has been lost so I'm not totally
clear whats going on.

BUT it does look like you have posted the class definition
but not the driver code that actually causes the error.

> class Game:
> def __init__(self,screen):
> print player.Player('foo')     # this line works fine, prints out a
> self.setupPlayers()               # want to set up all the players so...
>self.screen = screen

>def setupPlayers(self):
> print player.Player('bar') # this line fails, with the unbound local error

> Ouput and traceback:
>
> <player.Player instance at 0x00AD3F58>
> Traceback (most recent call last):
>   File "C:\Python24\sup.py", line 199, in ?
>     game = Game(screen)

where is this line? its nowhere above?
is it in a separate file? or lower in the same file?
It looks like its the same file but much later on.

>  File "C:\Python24\sup.py", line 56, in setupPlayers
>    print player.Player('bar')
> UnboundLocalError: local variable 'player' referenced before assignment

which looks like the import hasn't worked, except the previous print
statement did work. That seems odd I agree.

> Something blindingly obvious probably, but I'm at a loss.

Me too, but I'd start by checking I don;t have a conflict of variable names.
you don't for example del(player) at some point?

strange,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list