7.1 function accepts any number of parameters
def get_attr(first, *args):
print(*args)
7.2 Zhi function accepts keyword arguments
def get_attr(first, *args, k, **kwargs):
print(k)
get_attr(1, 'halo', 'halo2', k=2, w=3, d=4)
7.3 to the function parameter increases meta-information
def add(x: int, y: int) -> int:
return x + y
7.4 function returns a plurality of values
def add():# 返回元组
return 1, 2, 3
a, b, _ = add() # 元组拆包
7.5 -defined functions have default parameters
def add(i=4):
return 1, 2, i
7.6 define anonymous functions
names = ['David Beazley', 'Brian Jones', 'Raymond Hettinger', 'Ned Batchelder']
sorted(names, key=lambda name: name.split()[-1].lower())
7.7 anonymous function captures variable values
Rather than capture the definition at runtime