Skip to content

Instantly share code, notes, and snippets.

@JeffCohen
Created November 14, 2015 18:09
Show Gist options
  • Save JeffCohen/848e758f599a4c69c9fa to your computer and use it in GitHub Desktop.
Save JeffCohen/848e758f599a4c69c9fa to your computer and use it in GitHub Desktop.
Odd scoping in Python
# I don't understand the output (shown below).
# L is local to f, right? So how are the
# contents of L remembered between invocations?
def f(a, L=[]):
L.append(a)
return L
print(f(1))
print(f(2))
print(f(3))
# Output:
# [1]
# [1, 2]
# [1, 2, 3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment