The variable args with an asterisk (*) will store all unnamed variable parameters, and args is a tuple; and the variable kwargs with ** will store named parameters, that is, parameters in the form of key=value, and kwargs is a dictionary.
>>> def fun(a, b, *args, **kwargs): ... """可变参数演示示例""" ... print "a =", a ... print "b =", b ... print "args =", args ... print "kwargs: " ... for key, value in kwargs.items(): ... print key, "=", value ... >>> fun(1, 2, 3, 4, 5, m=6, n=7, p=8) # 注意传递的参数对应 a = 1 b = 2 args = (3, 4, 5) kwargs: p = 8 m = 6 n = 7 >>> >>> >>> >>> c = (3, 4, 5) >>> d = {"m":6, "n":7, "p":8} >>> fun(1, 2, *c, **d) # 注意元组与字典的传参方式 a = 1 b = 2 args = (3, 4, 5) kwargs: p = 8 m = 6 n = 7
2.阶乘 递归函数
For simple functions, there is also a convenient way of expressing them, namely: lambda expressions
# ###################### 普通函数 ######################
# 定义函数(普通方式)
def func(arg):
return
arg + 1
# 执行函数
result = func(123)
# ###################### lambda ######################
# 定义函数(lambda表达式)
my_lambda = lambda arg : arg + 1
# 执行函数
result = my_lambda(123)