Created
October 15, 2014 03:22
-
-
Save johnpena/a8530de17de911e2056e to your computer and use it in GitHub Desktop.
Lexical scoping doesn't work like that in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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