做leetcode的时候注意到python3的函数定义和2不太一样。
举例:
python2
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
python3
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
对比发现是python3.x引入的函数注释。
冒号标记:对函数参数的类型注释
箭头标记:对函数返回值得类型标记
其只含有标记作用,不对函数参数以及返回值进行类型检查。
函数注释的信息保存在__annotations__属性中,注释本身是一个字典类型的数据