#不定长参数,这里prams是一个元组集合
def print_params(*prams): for e in prams: print(e) print(prams) #输出('xxx', (1, 2, 3), 'hello')print_params("xxx", (1,2,3), "hello")
#关键字参数,使用参数名提供的参数,主要用于明确每个参数的作用,例如
def hello_print(greeting = "hello", name = "world"): print('%s %s' % (greeting, name) )hello_print() #输出hello world
hello_print("hello", "sysnap") #输出hello sysnaphello_print(name = "hello", greeting = "sysnap") #输出sysnap hello#不定长参数 + 关键字参数
#print_params(name = "hello") 这样会出错,如何避免呢,需要**def print_params_2(*params1, **params): for e in params1: print(e) print(params1) #输出(1, 2, 3) print(params) #输出{'name': 'sysnap'},是一个字典print_params_2(1,2,3, name = "sysnap")
print_params_2(1,2,3, name = "sysnap", age = 10)#(1, 2, 3)#{'age': 10, 'name': 'sysnap'}#反转过程
def add(x, y): return x + yprint(add(10,20)) #输出30,也可以下面的调用
params = (10, 20)print(add(*params))def print_str(name, age):
print("name is: %s, age is %d" % (name, age))del paramsparams = {"name":"sysnap", "age":10}print_str(**params)