def hello(greet : str, flag : bool) -> int: print('hello', greet)print(hello.__annotations__)#输出函数hello的属性#输出{'greet': <class 'str'>, 'flag': <class 'bool'>, 'return': <class 'int'>}def hello(greet:'hello', flag:'world') -> 'helloworld': print('hello', greet) return print(greet)print(hello.__annotations__) #输出{'greet': 'hello', 'flag': 'world', 'return': 'helloworld'}
Python函数后面的箭头一般用在函数后面,用来注释返回值是个什么东西
函数注释包括:
参数注释:以冒号(:)标记
返回值注释:以 -> 标记
用一句话hello.__annotations__就得到了hello函数属性的类型。
需要注意,注解对Python解释器没任何意义,python对不做检查,不做强制,不做验证!什么都不做。所以注释后面即使是一段话(字符串)也OK!
联系客服