Skip to content

Instantly share code, notes, and snippets.

@davidism
Created July 27, 2016 15:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidism/645100dc43633c02d60f156ef4d0d451 to your computer and use it in GitHub Desktop.
Save davidism/645100dc43633c02d60f156ef4d0d451 to your computer and use it in GitHub Desktop.
class namespace scope
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
@davidism
Copy link
Author

davidism commented Jul 27, 2016

The results should be less surprising the further down you go. Comprehensions are the most surprising, but they're basically implemented as functions. Lambdas are functions, and by the time you get to the static (or class, or instance) method this should look completly normal. Functions / methods don't use the class namespace for name lookup.

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