python中def函數右側有個->的含義

在有->的情況下:

def f(ham: str, eggs: str = 'eggs') -> str:
    print("Annotations:", f.__annotations__)
    print("Arguments:", ham, eggs)
    return ham + ' and ' + eggs


f('spam')

運行結果是:

# Annotations: {'ham': <class 'str'>, 'eggs': <class 'str'>, 'return': <class 'str'>}
# Arguments: spam eggs
 



def f(ham: str, eggs: str = 'eggs'):
    print("Annotations:", f.__annotations__)
    print("Arguments:", ham, eggs)
    return ham + ' and ' + eggs
f('spam')
 

運行結果是:

Annotations: {'ham': <class 'str'>, 'eggs': <class 'str'>}
Arguments: spam eggs

總結:

其實沒啥用,就是註釋的作用,挺花哨的功能.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章