Related to the topic, but not the implementation:
Thinking about scope, it's actually an interesting point of design
that Python doesn't allow referencing class variables from method
bodies. Things like metaclasses and dynamic subclassing in Python
would make it next to impossible to tell which variables are free,
since later mutations of the class object might cause those variables
to simply not exist anymore. Consider:
def f(name, bases, dict):
dict.__delitem__("x")
x = "global x"
class C():
x = "class var"
__metaclass__ = f
def m(self):
return x
If the reference to x above was allowed, this would be a weird
situation for `x' indeed. Is it simply undefined? Does it drop
through to "global x"? Does it still manage to capture the "class
var" value? Python's class scope, while weird (as we've noticed),
does neatly sidestep these particular issues.