Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix 'NameError: name 'field' is not defined' [closed]

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

like image 748
Kathia Avatar asked Aug 30 '19 14:08

Kathia


People also ask

How do you solve NameError name is not defined?

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.

How do you resolve a name error in Python?

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.

Why am I getting a NameError in Python?

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.

Which operator is used to make a name undefined in Python?

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.


1 Answers

You need to import field() to use it in your code:

from dataclasses import dataclass, field
like image 125
Ralf Avatar answered Oct 20 '22 17:10

Ralf