在网站中搜索关键字seo常见的优化技术
args
用于表示可变数量的位置参数(Positional Arguments)
kwargs
用于表示可变数量的关键字参数(Keyword Arguments)。
*args 来接收不定数量的位置参数
实例:
def exampleFunc(*args):print(type(args))for arg in args:print(arg)exampleFunc("123","testArgs","456",789)
输出结果:
<class 'tuple'>
123
testArgs
456
789
exampleFunc函数定义中的 *args 表示该函数可以接受任意数量的位置参数。在函数体内部,我们使用 args 来遍历位置参数元组
(传递进来的args是元组类型),并打印每个参数。
**kwargs 来接收不定数量的关键字参数
在函数定义中,**kwargs 表示将传递给函数的关键字参数打包成一个字典
。这样,函数在接收到关键字参数时,可以通过字典的方式访问这些参数。
实例:
def exampleFunc(**kwargs):for key, value in kwargs.items():print(key, value)exampleFunc(name='Li', age=25, city='New York')
运行结果:
name Li
age 25
city New York
exampleFunc() 函数定义中的 **kwargs 表示该函数可以接受任意数量的关键字参数。在函数体内部,我们使用 kwargs.items() 来遍历关键字参数字典,并打印每个键值对。
应用练习
同时组合使用,这种场景一般在我们定义装饰器的时候经常见到:
def testArgs(*args, **kwargs):print(args)print(type(args))print(kwargs)print((type(kwargs))testArgs(21312, 123123, a=12313, b=2)
输出:
(21312, 123123)
<class 'tuple'>
{'a': 12313, 'b': 2}
<class 'dict'>
定义一个计算函数运行的装饰器:
from time import time, sleepdef timmer(func):def calcTime(*args, **kwargs):startTime = time()func(*args, **kwargs)endTime = time()print('This func used ', endTime - startTime, 's')return endTime - startTimereturn calcTime@timmer
def testTimmer():sleep(1)testTimmer()
执行结果:
This func used 1.002957820892334 s
同样的,对于接收不定长参数的函数,我们可以打包入参,函数里的*
和 **
分别进行列表和字典的解包操作,将它们作为函数的参数传递进去。比如:
def add(a, b):print(f'{a}+{b} = {a+b}')tup = (222,333)
dic = {"a": 122, "b": 233}add(dic["a"], dic["b"])
add(**dic)
add(*tup)
执行结果:
122+233 = 355
122+233 = 355
222+333 = 555
最后,要说明的是,args和kwargs是可以替换成别的字符:
def testArgs(*AAA, **BBB):print(AAA)print(BBB)testArgs(21312, 123123, a=12313, b=2)
执行结果 :
(21312, 123123)
{'a': 12313, 'b': 2}