Created
July 27, 2016 15:03
-
-
Save davidism/645100dc43633c02d60f156ef4d0d451 to your computer and use it in GitHub Desktop.
class namespace scope
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
a = 'global' | |
class Fred: | |
a = 'class' | |
b = (a for i in range(10)) | |
c = [a for i in range(10)] | |
d = a | |
e = lambda: a | |
f = lambda a=a: a | |
@staticmethod | |
def g(): | |
return a | |
print(Fred.a) # class | |
print(next(Fred.b)) # global | |
print(Fred.c[0]) # class in Python 2, global in Python 3 | |
print(Fred.d) # class | |
print(Fred.e()) # global | |
print(Fred.f()) # class | |
print(Fred.g()) # global |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A comprehensive explanation: http://stackoverflow.com/questions/13905741/accessing-class-variables-from-a-list-comprehension-in-the-class-definition/13913933#13913933