The fact that you assign a name Fooinside the class definition makes the name a Foolocal name in this scope (i.e. the scope of the class). Local names are defined statically during parsing and compilation into byte code. When execution reaches approval
Foo = Foo
Python first evaluates the right side. It searches for a local name Foo- as determined at compile time - and does not find it in the local scope. Hence the error.
The same thing happens if you try
def test_foo():
foo = 3
class A:
bar = foo
foo = 42
test_foo()
source
share