Question

class A(object):
    class B(object): pass
    class C(A.B): pass

results in

NameError: name 'A' is not defined

How do I inherit from B in C, if they are adjacent , both nested in A (inner classes)?

Was it helpful?

Solution

You cannot use A until the class body has finished executing.

You can refer to 'local' names; the class body is executed as a function, and the local namespace of that function is used to supply the class attributes; within the class body, B is a local name:

class A(object):
    class B(object): pass
    class C(B): pass
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top