Skip to content

Instantly share code, notes, and snippets.

@saghul
Created June 16, 2011 06:46
Show Gist options
  • Save saghul/1028793 to your computer and use it in GitHub Desktop.
Save saghul/1028793 to your computer and use it in GitHub Desktop.
Name binding and UnboundLocalError
from application.system import host
class Foo(object):
def __init__(self, host_ip=None):
self.host_ip = host_ip or host.default_ip
for host in range(10):
print host
f = Foo()
print f.host_ip
# Error given by the above code
#
#Traceback (most recent call last):
# File "bogus_scope.py", line 11, in <module>
# f = Foo()
# File "bogus_scope.py", line 7, in __init__
# self.host_ip = host_ip or host.default_ip
#UnboundLocalError: local variable 'host' referenced before assignment
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment