Would there be a simple way to fix this error while keeping all 3 levels?
Deriving ClassA from object does not help.
Thanks in advance!
>>> class classA:
... class classB(object):
... def __init__(self):
... self.b = 3
... class classC(classA.classB):
... def __init__(self):
... super(classC, self).__init__()
... self.c = 4
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in classA
File "<stdin>", line 5, in classB
NameError: name 'classA' is not defined
2 Answers 2
No. At the time you define classC, classA does not exist yet.
It is only created after its body is fully executed. (The dict created from the body's execution is one parameter for the class creation call class = type('classname', (superclass_1, superclass_2, superclass_3), said_dict}).)
The easiest way would be defining them at the same level.
If absolutely needed, you can modify them later:
class classA:
pass
class classB(object):
def __init__(self):
self.b = 3
class classC(classB):
def __init__(self):
super(classC, self).__init__()
self.c = 4
classA.classB = classB
classB.classC = classC
1 Comment
Maybe something like this would work:
class classA:
pass
class classB(object):
def __init__(self):
self.b = 3
classA.classB = classB
class classC(classA.classB):
def __init__(self):
super(classC, self).__init__()
self.c = 4
classA.classB.classC = classC
classA.classB.classCis no way shorter thanclassC...