Python 3.x “def twoSum(self,nums:List[int],target:int)”的机制是什么;List[int]:`在python 3中:

Python 3.x “def twoSum(self,nums:List[int],target:int)”的机制是什么;List[int]:`在python 3中:,python-3.x,function,arguments,Python 3.x,Function,Arguments,I在python3中找到如下代码: def twoSum(self, nums: List[int], target: int) -> List[int]: return sum(nums) 正如我所知,对于pythondef,我们只需要以下内容: def twoSum(self, nums, target): return sum(nums) 什么是nums:List[int]、target:int和->List[int]的意思?这些是Python3的新特性吗?我从来

I在python3中找到如下代码:

def twoSum(self, nums: List[int], target: int) -> List[int]:
    return sum(nums)
正如我所知,对于python
def
,我们只需要以下内容:

def twoSum(self, nums, target):
    return sum(nums)
什么是
nums:List[int]、target:int
->List[int]
的意思?这些是Python3的新特性吗?我从来没见过那些


谢谢,

这是python中用于类型检查的静态类型。它允许您定义输入参数和返回的类型,以便预先处理某些不兼容性。它们只是注释,而不是实际的静态类型。查看mypy软件包了解更多信息

from typing import List

def twoSum(nums: List[int], target: int) -> List[int]:
    print(nums, target)
链接:

注意Python运行时不强制函数和变量类型注释。它们可以由第三方工具使用,如类型检查器、IDE、linter等


链接:

在使用了输入导入列表中的
之后,我仍然遇到
目标:int
的问题:
类型错误:twoSum()缺少1个必需的位置参数:“target”
;有什么想法吗?