Skip to content

Instantly share code, notes, and snippets.

@kevinschaul
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinschaul/616ac644941a84048021 to your computer and use it in GitHub Desktop.
Save kevinschaul/616ac644941a84048021 to your computer and use it in GitHub Desktop.
Python scope probs
my_map = {}
crazier_tho = {}
def main():
my_map = {
'hello': 'hi'
}
print my_map
printMyMap()
crazier_tho['hello'] = 'hi'
print crazier_tho
printCrazierTho()
def printMyMap():
print my_map
def printCrazierTho():
print crazier_tho
if __name__ == '__main__':
main()
# Prints:
# {'hello': 'hi'}
# {}
# {'hello': 'hi'}
# {'hello': 'hi'}
@sethblanchard
Copy link

Maybe this: http://stackoverflow.com/a/292502
Look at the comment a bit down:
"As a caveat to Global access - reading a global variable can happen without explicit declaration, but writing to it without declaring global(var_name) will instead create a new local instance."

It seems like you are creating a local variable that hides the global

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