天天看點

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