UnboundLocalError

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 19
Reputation: jcafaro10 is an unknown quantity at this point 
Solved Threads: 0
jcafaro10 jcafaro10 is offline Offline
Newbie Poster

UnboundLocalError

 
0
  #1
Dec 2nd, 2008
This is probably an easy problem to solve but I'm having a bit of difficulty. The error is UnboundLocalError: local: "switch" and that's all the error says. The line refers to the line switch = - switch in the following code:

  1. boundary = game.getBoundary()
  2. game.setBackground(java.awt.Color.black)
  3.  
  4. level = 1
  5. maxX = -1
  6. maxItem = 0
  7. minX = boundary.getWidth()+1
  8. minItem = 0
  9. change = 0
  10. switch = 1
  11.  
  12. class shootCollisionListener (events.CollisionListener):
  13. global switch
  14. def __init__ (self, item):
  15. self._item = item
  16. def collisionDetected(self, event):
  17. if(event.getSource()==boundary):
  18. switch = -switch
  19. elif(event.getSource().getType()=="projectile"):
  20. game.removeElement(event.getSource())
  21. game.removeElement(self._item)

I used global so I don't know why I'm getting this error.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: UnboundLocalError

 
0
  #2
Dec 2nd, 2008
When variables are assigned inside a function, they are always bound to the function's local namespace. The global namespace is never checked for name switch, therefore the error.
Example:
  1. >>> switch = -1
  2. >>> def test():
  3. ... return -switch
  4. ...
  5. >>> switch = test()
  6. >>> switch
  7. 1
  8. >>> def test1():
  9. ... switch = -switch
  10. ... return switch
  11. ...
  12. >>> test1()
  13. Traceback (most recent call last):
  14. File "<interactive input>", line 1, in ?
  15. File "<interactive input>", line 2, in test1
  16. UnboundLocalError: local variable 'switch' referenced before assignment
  17. >>> def test2():
  18. ... global switch
  19. ... switch = -switch
  20. ... return switch
  21. ...
  22. >>> test2()
  23. -1
  24. >>> test2()
  25. 1
  26. >>>
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 960
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 220
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: UnboundLocalError

 
0
  #3
Dec 2nd, 2008
The local namespace of a method doesnt see the namespace of the class. Inside a method, you can only access the variables from the local namespace and the variables defined at module level. So your global switch is misplaced.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 19
Reputation: jcafaro10 is an unknown quantity at this point 
Solved Threads: 0
jcafaro10 jcafaro10 is offline Offline
Newbie Poster

Re: UnboundLocalError

 
0
  #4
Dec 2nd, 2008
That makes sense. Works now! Thanks
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC