Python中参数的冒号与箭头表示注释

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/Stitchez/p/10006519.html

猜你喜欢

转载自www.cnblogs.com/sbj123456789/p/10718288.html