NameError: name 'List' is not defined

后端 未结 3 858
情深已故
情深已故 2020-12-29 18:01

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.



        
3条回答
  •  情话喂你
    2020-12-29 18:44

    Since Python 3.9, you can use built-in collection types (such as list) as generic types, instead of importing the corresponding capitalized types from typing.
    This is thanks to PEP 585

    So in Python 3.9 or newer, you could actually write:

    def totalFruit(self, tree: list[int]) -> int: # Note list instead of List
        pass
    

    without having to import anything.

提交回复
热议问题