Created
November 14, 2015 18:09
-
-
Save JeffCohen/848e758f599a4c69c9fa to your computer and use it in GitHub Desktop.
Odd scoping 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
# 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