我真的不确定为什么这不起作用。这是代码的重要部分(来自 leetcode 挑战赛)。 第一行抛出 NameError。
def totalFruit(self, tree: List[int]) -> int:
pass
如果我首先尝试导入List
,则会收到错误No module named 'List'
。我正在使用 Anaconda 的 Python 3.7.3。
最佳答案
为了能够注释列表应接受的类型,您需要使用 typing.List
from typing import List
那么你导入了List
吗?
更新
如果您使用的是 Python > 3.9,see @Adam.Er8's answer
关于python - 名称错误 : name 'List' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57505071/