函数
函数定义 使用def关键词。
#!/usr/bin/env python
def another_function():
print('Another function')
def main():
print('Hello, World!')
another_function()
if __name__ == '__main__':
main()
函数名虽然可以自由定义,但习惯上使用蛇形命名法(snake_case),即小写的英数字以 _ 连接。
提示
函数定义是一般习惯在函数之间保持两空白行。
参数
往函数里传参数。
def another_function(x, y):
print(f'The value of x is {x}')
print(f'The value of y is {y}')
def main():
another_function(5, 6)
有默认值的参数
如果调用函数时不明确使用有默认值的参数,参数则使用默认值。
def another_function(x, y=10):
print(f'The value of x is {x}')
print(f'The value of y is {y}')
def main():
another_functin(5, 3) # x: 5, y: 3
another_functin(7) # x: 7, y: 10
有默认值的参数应该放到函数参数列表的最后。
# OK
def f1(x, y=10):
print(f'The value of x is {x}')
print(f'The value of y is {y}')
# OK
def f2(x, y=10, z=20):
print(f'The value of x is {x}')
print(f'The value of y is {y}')
print(f'The value of z is {z}')
# NG
def f3(y=10, x, z=20):
print(f'The value of x is {x}')
print(f'The value of y is {y}')
print(f'The value of z is {z}')
提示
有默认值参数的定义的时候习惯上等号两边不留空格。
有命名的参数
当调用的函数使用有命名的参数时,调用该函数时可以不考虑参数的顺序。
def another_function(x, y):
print(f'The value of x is {x}')
print(f'The value of y is {y}')
def main():
another_function(y=10, x=20)
*args, **kwargs
函数的参数定义使用* 标记时,则可以使用不受个数限制的参数,同时这些参数将被打包成一个元组使用。
def another_function(x, *args):
print(f'The value of x is {x}') # x: 0
print(f'The value of args is {args}') # args: (1, 2, 3)
def main():
another_function(0, 1, 2, 3)
另外,参数名前以**标记后,则可以使用不受个数限制个数不受限制的有命名参数,,同时这些参数将被打包成一个字典使用。
def another_function(x, **kwargs):
print(f'The value of x is {x}') # x: 0
print(f'The value of kwargs is {kwargs}') # kwargs: {'y': 10, 'z': 20}
def main():
another_function(0, y=10, z=20)
*args, **kwargs 可以同时使用。
def another_function(x, *args, **kwargs):
print(f'The value of x is {x}') # x: 0
print(f'The value of args is {args}') # args: (1, 2)
print(f'The value of kwargs is {kwargs}') # kwargs: {'y': 10, 'z': 20}
def main():
another_function(0, 1, 2, y=10, z=20)
提示
* 参数和 ** 参数的命名虽然没有硬性要求,但习惯上使用 *args, **kwargs 。
参数的解包
给函数传参如果在元组前标注* ,则会自动解包这个元组,相当于把元组里的各元素一个个传参调用。
def another_function(x, y, z):
print(f'The value of x is {x}') # x: 0
print(f'The value of y is {y}') # y: 1
print(f'The value of z is {z}') # z: 2
def main():
args = (0, 1, 2)
another_function(*args) # another_function(0, 1, 2)
给函数传参如果在字典前标注** ,则会自动解包这个字典,相当于把字典的各元素作为命名参数一个个传参调用。
def another_function(x, y, z):
print(f'The value of x is {x}') # x: 0
print(f'The value of y is {y}') # y: 1
print(f'The value of z is {z}') # z: 2
def main():
kwargs = {'x': 0, 'y': 1, 'z': 2}
another_function(**kwargs) # another_function(x=0, y=1, z=2)
参数类型声明
如果给参数指定类型,则调用函数时必须使用指定类型的参数。
def another_function(x: int, y: int):
print(f'The value of x * y is {x * y}')
如果不给参数指定类型,那么函数传参类型将不受限制。不过过于随意是混乱之源,因此推荐给参数指定类型。
函数返回值
函数调用,可以返回值给调用方。使用return。
def plus_one(x):
return x + 1
def main():
x = plus_one(5) # x: 6
print(f'The value of x is {x}')
返回值可以有多种类型。
def another_function(x):
if x == 0:
return x + 3.14
else:
return x + 1
这同样也可能导致混乱,因此推荐给函数返回值指定类型。
def plus_one(x: int) -> int:
return x + 1
Python函数一定有返回值,即使不使用return返回值将为None。
def f(x):
print(f'The value of x is {x}')
这个函数和下面的函数有相同的返回值。
def f(x):
print(f'The value of x is {x}')
return None
空函数
函数体为空的时候可以使用pass
# 什么都不做
def empty_function():
pass