How to fix 'NameError: name 'field' is not defined'

How to fix 'NameError: name 'field' is not defined'

在我尝试使用字段 nxy 创建 class dots 时,如下所示:

from dataclasses import dataclass

@dataclass
class dots:
    n: int = 200
    xy: List[int] = field(default_factory=list)

我经常收到错误消息:

NameError: name 'field' is not defined

关于如何修复它有什么想法吗?

我的操作系统是Ubuntu 18.04.3 LTS,内核版本是4.15.0-58-generic。我正在使用 Python 3.6.4

您需要导入 field() 才能在您的代码中使用它:

from dataclasses import dataclass, field