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

总结:

其实没啥用,就是注释的作用,挺花哨的功能.

发布了758 篇原创文章 · 获赞 343 · 访问量 162万+

猜你喜欢

转载自blog.csdn.net/appleyuchi/article/details/104770430