Now the following code works perfectly with Python 3.7
class A:
class B:
def __init__(self):
print("B")
class C:
def __init__(self):
self.b = A.B()
def main():
a = A.C()
if __name__ == "__main__":
main()
It prints a B on the screen.
However, with a small modification that tries to introduce dataclass, the code cannot run well.
from dataclasses import dataclass
class A:
class B:
def __init__(self):
print("B")
@dataclass
class C:
b = A.B()
def main():
a = A.C()
if __name__ == "__main__":
main()
Python reports -- for b = A.B() -- NameError: name 'A' is not defined.
Does anyone know how to fix this issue to achieve the same result with dataclass? And why does it say name 'A' is not defined?
A.C = C. Note, your dataclass is not equivalent to what you had before.