Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
LeetCode Python3函数注释在Visual Studio代码中不起作用_Python_Visual Studio Code - Fatal编程技术网

LeetCode Python3函数注释在Visual Studio代码中不起作用

LeetCode Python3函数注释在Visual Studio代码中不起作用,python,visual-studio-code,Python,Visual Studio Code,我希望能够从LeetCode复制并粘贴一个启动函数,并在VisualStudio代码中运行它 LeetCode支持如下函数注释: class Solution: def printWords(self, wordList: List[str]) -> None: for word in wordList: print(word) Solution().printWords(["cat", "dog", "cow", "bird"]) 但如

我希望能够从LeetCode复制并粘贴一个启动函数,并在VisualStudio代码中运行它

LeetCode支持如下函数注释:

class Solution:
    def printWords(self, wordList: List[str]) -> None:
        for word in wordList:
            print(word)

Solution().printWords(["cat", "dog", "cow", "bird"])
但如果我将其完全粘贴到VisualStudio代码中,我会得到一个错误。显然,我可以删除函数注释,它就会工作,这是我通常做的。然而,如果我能找到解决这个问题的办法,那就太酷了

特别是,只要我有List[int]或List[People]又称List[object],它就会失败

也许LeetCode在做一些非标准的事情

虽然我以为我在读到PEP-0526中介绍了这一点,但我可能错了


有人知道如何解决这个问题吗?谢谢

在我的计算机上运行时,(我没有使用Visual Studio代码,因此您可能会遇到其他错误),但我得到的错误是NameError:name'List'未定义。所以我导入了列表类型:

from typing import List

class Solution:
    def printWords(self, wordList: List[str]) -> None:
        for word in wordList:
            print(word)

Solution().printWords(["cat", "dog", "cow", "bird"])
它的工作原理与预期一致


编辑:我在通读之后更新了我的解决方案,以便更准确地描述该语法,并获得关于该主题的文档

错在哪里?谢谢!这在我当前环境中的VisualStudio代码中也适用。我们也非常感谢文档的链接。@DavidPavel很高兴听到这个消息。如果我的答案解决了你的问题,请将其标记为答案,并/或按照