Skip to content

Instantly share code, notes, and snippets.

@xevix
Last active June 22, 2017 18:40
Show Gist options
  • Save xevix/d3082a0372174f8e624415f7fb395f9d to your computer and use it in GitHub Desktop.
Save xevix/d3082a0372174f8e624415f7fb395f9d to your computer and use it in GitHub Desktop.
Iterator accessible after its scope ends
def main():
ary = ['I should never print']
for elem in ary:
pass
other_ary = ['other']
for other_elem in other_ary:
# "elem" hould not be defined here, but this works, because of Python's awful scoping rules
# which scope all variable declarations to the function scope
# See: http://eli.thegreenplace.net/2015/the-scope-of-index-variables-in-pythons-for-loops/
print elem
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment