In my attempt to create a class dots
with the fields n
and xy
as shown below:
from dataclasses import dataclass
@dataclass
class dots:
n: int = 200
xy: List[int] = field(default_factory=list)
I am constantly getting the error :
NameError: name 'field' is not defined
Any ideas on how to fix it?
My operating system is Ubuntu 18.04.3 LTS, and the kernel version 4.15.0-58-generic. I am using Python 3.6.4
The Python "NameError: name is not defined" occurs when we try to access a variable or function that is not defined or before it is defined. To solve the error, make sure you haven't misspelled the variable's name and access it after it has been declared.
To specifically handle NameError in Python, you need to mention it in the except statement. In the following example code, if only the NameError is raised in the try block then an error message will be printed on the console.
What Is a NameError in Python? In Python, the NameError occurs when you try to use a variable, function, or module that doesn't exist or wasn't used in a valid way. Some of the common mistakes that cause this error are: Using a variable or function name that is yet to be defined.
Python uses the keyword None to define null objects and variables. While None does serve some of the same purposes as null in other languages, it's another beast entirely. As the null in Python, None is not defined to be 0 or any other value.
You need to import field()
to use it in your code:
from dataclasses import dataclass, field
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With