【发布时间】:2019-09-21 01:13:46
【问题描述】:
这是我在 leetcode.com 上的 Python3 代码框架中看到的语法。注意函数输入参数的类型声明(或者至少我认为它们是类型声明,以前从未见过)。 nums 必须是int 的List,并且s 必须是int。
class Solution:
def findTargetSumWays(self, nums: List[int], S: int) -> int:
pass
如果我在 leetcode.com 的环境中运行该函数,它会退出且不会出错。但是,如果我在自己的 Python 3.7.3 环境中运行相同的代码,则会收到 NameError。
def findTargetSumWays(self, nums: List[int], S: int) -> int: NameError: name 'List' is not defined
怎么了? leetcode.com 上的语法是真正的 Python 吗?
【问题讨论】:
-
您可以从
typing模块导入List。 -
技术术语是函数注释,其中一种用途(也是唯一官方支持的用途)用于类型提示。
标签: python python-3.x types nameerror