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'>}

 

posted @ 2019-09-21 13:06  木林森__𣛧  阅读(545)  评论(0编辑  收藏  举报