Skip to content

Instantly share code, notes, and snippets.

@motivic
Created August 26, 2016 00:41
Show Gist options
  • Select an option

  • Save motivic/73a66f39f6ddc409b23cf7eb72f2059b to your computer and use it in GitHub Desktop.

Select an option

Save motivic/73a66f39f6ddc409b23cf7eb72f2059b to your computer and use it in GitHub Desktop.
Python lexical scoping
# https://google.github.io/styleguide/pyguide.html
i = 4
def foo(x):
def bar():
print i,
# ...
# A bunch of code here
# ...
for i in x: # Ah, i *is* local to Foo, so this is what Bar sees
print i,
bar()
@motivic
Copy link
Author

motivic commented Aug 26, 2016

So foo([1, 2, 3]) will print 1 2 3 3, not 1 2 3 4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment