Skip to content

Instantly share code, notes, and snippets.

@eykd
Created October 11, 2016 21:36
Show Gist options
  • Save eykd/c63a7cf760a538ee8bc3828362ed12e3 to your computer and use it in GitHub Desktop.
Save eykd/c63a7cf760a538ee8bc3828362ed12e3 to your computer and use it in GitHub Desktop.
Demonstrating what appears to be a scoping bug in Python 3.5.1
from __future__ import print_function
foo = 'bar'
# This works
[foo for _ in range(5)]
# This also works:
class Foo:
bar = [foo for _ in range(5)]
# This does not work in Python 3.5.1:
class Bar:
bar = foo
blah = [bar for _ in range(5)]
print("Success!")
# $ pyenv local 2.7.11
#
# $ python comp_scope_bug.py
# Success!
#
# $ pyenv local 3.5.1
#
# $ python comp_scope_bug.py
# Traceback (most recent call last):
# File "comp_scope_bug.py", line 13, in <module>
# class Bar:
# File "comp_scope_bug.py", line 15, in Bar
# blah = [bar for _ in range(5)]
# File "comp_scope_bug.py", line 15, in <listcomp>
# blah = [bar for _ in range(5)]
# NameError: name 'bar' is not defined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment