T
Terry Reedy
Lie said:Note that when the python interpreter meets this statement:
class B(P):
def foo(self):
print('ab')
X = 'f'
the compiler sees a class statement -> create a new blank class
-> assign P as the new class' parent
No, it saves the name 'B' and bases tuple P, and create a new *dict*,
call it d here though it is anonymous as far as the class body is concerned.
-> *execute* the class' body
with the new dict d as the local namespace. In other words, the
equivalent of
exec('''body''', globals(), {}) # new Py3 exec() form
# new context
the compiler sees a def statement -> *compile* the def's body
to a code object which is then attached to a function object
-> assign the compiled body to B.foo
No said:the compiler sees X = 'f' statement -> assign 'f' to B.X
No, d['X'] = 'f'
# exit the new context
Up to this point, there is no new class object.
-> assign the new class to B
It calls meta(name, bases, d), where meta is the metaclass 'type' by
default but can be any callable which does anything, though the
intention is that it be a subclass of type or at least something that
creates a class object, and that d become the backstage attribute dict
for the result.
Your problem is related to how the interpreter *execute* a class
definition rather than the name resolution.
Terry Jan Reedy