[Tutor] UnboundLocal Error

Jason Massey jason.massey at gmail.com
Sun Nov 20 22:54:09 CET 2005


I've got a problem with scope that I can't say I've ever encountered.

The comments explain the situation pretty well.


import player

class Game:
	def __init__(self,screen):
		self.market = Market.Market()
		self.countries = pickle.load(open("sup.coords",'r'))
		self.deck = deck.Deck()
		self.players = {}
		print player.Player('foo')     # this line works fine, prints out a
player instance
		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
		for super_power in SUPER_POWERS:
			self.players[super_power] = player.Player(super_power)
			for position,card in enumerate(self.deck):
				if card.location in SUPER_POWERS[super_power]:
					

Ouput and traceback:

<player.Player instance at 0x00AD3F58>
Traceback (most recent call last):
  File "C:\Python24\sup.py", line 199, in ?
    game = Game(screen)
  File "C:\Python24\sup.py", line 51, in __init__
    self.setupPlayers()
  File "C:\Python24\sup.py", line 56, in setupPlayers
    print player.Player('bar')
UnboundLocalError: local variable 'player' referenced before assignment


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

thanks,

jason


More information about the Tutor mailing list