NameError: name 'Class_Name' is not defined
Question:
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.
Answers:
from pprint import pp
class FirstClass():
#for typing, if you want to indicate a class variable
second_class: type["SecondClass"]
#for typing, if you want to indicate an instance variable
second_class_instance: "SecondClass"
def __init__(self):
"""SOME CODE HERE"""
self.second_class_instance = self.second_class()
#this looks off in the inheritance declaration - dont specify types here
#class SecondClass(firsClass: FirstClass):
class SecondClass(FirstClass):
def __init__(self):
#this code will error, watch your cases!
#self.first_class = firstClass
...
#for doing something with the second_class attribute, later in the runtime
#however, no tricks with "SecondClass" quoting allowed, you need
#to wait till both FirstClass and SecondClass EXIST, which is why
#this line is here, with this indentation
FirstClass.second_class = SecondClass
first = FirstClass()
pp(first)
pp(vars(first))
Is doing this a good idea? Probably not, but may there is special, special, justification for tracking a second_class
class reference, say to call a class constructor without hardcoding the SecondClass in FirstClass methods.
And, in this scheme, I am even less convinced by having SecondClass subclass FirstClass, but…
output:
% py test_429_declare.py
<__main__.FirstClass object at 0x10841f220>
{'second_class_instance': <__main__.SecondClass object at 0x10841f250>}
and mypy
% mypy test_429_declare.py
Success: no issues found in 1 source file
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
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.
from pprint import pp
class FirstClass():
#for typing, if you want to indicate a class variable
second_class: type["SecondClass"]
#for typing, if you want to indicate an instance variable
second_class_instance: "SecondClass"
def __init__(self):
"""SOME CODE HERE"""
self.second_class_instance = self.second_class()
#this looks off in the inheritance declaration - dont specify types here
#class SecondClass(firsClass: FirstClass):
class SecondClass(FirstClass):
def __init__(self):
#this code will error, watch your cases!
#self.first_class = firstClass
...
#for doing something with the second_class attribute, later in the runtime
#however, no tricks with "SecondClass" quoting allowed, you need
#to wait till both FirstClass and SecondClass EXIST, which is why
#this line is here, with this indentation
FirstClass.second_class = SecondClass
first = FirstClass()
pp(first)
pp(vars(first))
Is doing this a good idea? Probably not, but may there is special, special, justification for tracking a second_class
class reference, say to call a class constructor without hardcoding the SecondClass in FirstClass methods.
And, in this scheme, I am even less convinced by having SecondClass subclass FirstClass, but…
output:
% py test_429_declare.py
<__main__.FirstClass object at 0x10841f220>
{'second_class_instance': <__main__.SecondClass object at 0x10841f250>}
and mypy
% mypy test_429_declare.py
Success: no issues found in 1 source file
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