class C(A, B, metaclass=meta): def f(self): return __class__
To be compiled approximately like this:
def _outer_C(*__args__, **__kw__): class _inner_C(*__args__, **__kw__): def f(self): return __class__ __class__ = _inner_C return _inner_C C = _outer_C(A, B, metaclass=meta)
My question is:
class A: def foo(a): class X: x = __class__ def fun(self): print(__class__) return X print(A().foo().x)
Why does this work? While the following doesn't?
def bar(): class Y: def fun(a): class X: m = n def foo(a): print(a.m) n = 1 return X n = 0 return Y if __name__ == '__main__': y = bar() x = y().fun() x().foo()
If the free variable is assigned in the outscope, it's turned to a cell var,
and because it's assigned after the class body is evaluated,
NameError is raised.
__class__ closure trick is really what it said to be, it should also raise a
what's the trick use to make it work?