Python 3函数的参数冒号注释

Python 3.7版本,函数的参数可以通过冒号来进行注释

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

str 这里都表示注释,而不是强制确定的类型(Python是动态类型的)

冒号后表示参数的注释,如果需要默认赋值再在后面加等号即可。

箭头后表示返回值的注释。

原文地址:https://www.cnblogs.com/Li-JT/p/15376165.html