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': , 'eggs': , 'return': }
# 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': , 'eggs': }
Arguments: spam eggs

总结:

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

 

你可能感兴趣的:(Python)