Skip to content

Instantly share code, notes, and snippets.

@reorx
Last active August 29, 2015 14:01
Show Gist options
  • Save reorx/8939a108b6f7cf5302ee to your computer and use it in GitHub Desktop.
Save reorx/8939a108b6f7cf5302ee to your computer and use it in GitHub Desktop.
>>> def foo():
... a = 1
... def foo_a():
... a = 2
... print 'foo_a a:', a
... foo_a()
... print 'foo a:', a
...
>>> foo()
foo_a a: 2
foo a: 1
>>> def foo():
... class A: pass
... a = A()
... b = 1
... def foo_a():
... a.c = b
... foo_a()
... print a.__dict__
... print a.c
...
>>> foo()
{'c': 1}
1
>> n = 1
>>> def foo():
... print n
...
>>> foo()
1
>>> n = 1
>>> def foo():
... n + 1
...
>>> foo()
>>> n = 1
>>> def foo():
... n = n + 1
...
>>> foo()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in foo
UnboundLocalError: local variable 'n' referenced before assignment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment