Skip to content

Instantly share code, notes, and snippets.

@johnpena
Created October 15, 2014 03:22
Show Gist options
  • Save johnpena/a8530de17de911e2056e to your computer and use it in GitHub Desktop.
Save johnpena/a8530de17de911e2056e to your computer and use it in GitHub Desktop.
Lexical scoping doesn't work like that in python
def decorator_function(original_function):
# x will not be accessible inside functions decorated by `decorator_function`
x = "certified organic and local"
def inner():
return original_function()
return inner
@decorator_function
def bar():
# nope
print x
bar()
# Result of running this code:
#Traceback (most recent call last):
# File "crap.py", line 14, in <module>
# bar()
# File "crap.py", line 6, in inner
# return original_function()
# File "crap.py", line 12, in bar
# print x
#NameError: global name 'x' is not defined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment