I have an issue which is I keep getting the error of NameError: name 'Class_Name' is not defined. Which I understand. The tricky part is that my code looks something like this:
class FirstClass():
second_class: SecondClass
def __init__(self):
"""SOME CODE HERE"""
class SecondClass(firsClass: FirstClass):
def __init__(self):
self.first_class = firstClass
So the problem is that no matter how I arrange the code, I will always have one above the other so it will always say that is not defined. How can I solve this?
------------------------SOLUTION----------------------------
I found the solution to this. If you use the import
from __future__ import annotations
It will read the file and you will be able to call the classes even if they are defined later in the code.
Actually, I found the solution to this.
If we use the import
from __future__ import annotations
it will read the file and it won't have that problem, so we can reference different things in the order that we want. Taking the example:
class FirstClass():
second_class: SecondClass
def __init__(self):
"""SOME CODE HERE"""
class SecondClass(firsClass: FirstClass):
def __init__(self):
self.first_class = firstClass
This will be resolved as:
from __future__ import annotations
class FirstClass():
second_class: SecondClass
def __init__(self):
"""SOME CODE HERE"""
class SecondClass(firsClass: FirstClass):
def __init__(self):
self.first_class = firstClass
You can find more details in the python documentation: https://docs.python.org/3/library/__future__.html