python 语法-参数注释

 python 语法-参数注释

最近碰到的这样的代码:

def func(a:"shuoming") -> int:

    print("函数已运行。")

func(34)

查阅得知它是一种新的参数注释方式,在3.5引入。

参数注释以冒号为分隔符,函数注释以->为分隔符。

这些信息保存在函数的__annotations__属性中。

它只是一种说明,并没有强制检查功能,下面演示一下与__doc__的不同。

def func(a:"shuoming", *ar:"shuoming2") -> int:

    """注释"""

    print("函数已运行。")

func(34)

print(func.__doc__)

print(func.__annotations__)

输出结果:

函数已运行。

注释

{'a': 'shuoming', 'ar': 'shuoming2', 'return': <class 'int'>}

猜你喜欢

转载自www.cnblogs.com/wodeboke-y/p/11562535.html