天天看点

Python 函数中箭头 (->)的用处

Python 3 -> 是函数注释的一部分,表示函数返回值的类型。

def useful_function(x) -> int:
    # Useful code, using x, here
    return x
           

可以通过函数的 annotations 看到。

>>> def useful_function(x) -> int:
...     # Useful code, using x, here
...     return x
...
>>> useful_function.__annotations__
{'return': <class 'int'>}
>>>
           

来源:https://zhuanlan.zhihu.com/p/264892455