NameError: name 'List' is not defined

泪湿孤枕 提交于 2019-12-19 16:12:15

问题


I'm really unsure why this isn't working. Here is the important part of the code (it's from a leetcode challenge). The first line throws the NameError.

def totalFruit(self, tree: List[int]) -> int:
    pass

If I try importing List first I get an error No module named 'List'. I'm using Python 3.7.3 from Anaconda.


回答1:


To be able to annotate what types your list should accept, you need to use typing.List

from typing import List

So did you import List?




回答2:


To be able to specify a list of str's in a type hint, you can use the typing package, and from typing import List (capitalized, not to be confused with the built-in list)



来源:https://stackoverflow.com/questions/57505071/nameerror-name-list-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!