Python 动态地将参数传递给函数
我需要跟踪集合中每个函数被调用的次数。如果在Python 动态地将参数传递给函数,python,Python,我需要跟踪集合中每个函数被调用的次数。如果在n秒内调用函数超过x次,我的程序需要暂停,然后重置该函数的计数 我的函数调用可能如下所示: a(1) b(1,参数2=2,参数3=3) c(1,参数3=3) 我的最佳想法是让包装器函数跟踪所有限制。差不多 def wrapper(function, function_params,x,n): if not hasattr(wrapper, "function_dict"): wrapper.function_dict = {}
n
秒内调用函数超过x
次,我的程序需要暂停,然后重置该函数的计数
我的函数调用可能如下所示:
a(1)
b(1,参数2=2,参数3=3)
c(1,参数3=3)
我的最佳想法是让包装器函数跟踪所有限制。差不多
def wrapper(function, function_params,x,n):
if not hasattr(wrapper, "function_dict"):
wrapper.function_dict = {}
if function not in wrapper.function_dict.keys():
wrapper.function_dict[function] = {
remaining = x, expires = time.time() + n
}
remaining = wrapper.function_dict[function]['remaining']
expires = wrapper.function_dict[function]['expires']
if remaining == 0:
time.sleep(expires - time.time())
wrapper.function_dict[function] = {
remaining = x, expires = time.time() + n
}
results = ????? # call function, this is what I don't know how to do
wrapper.function_dict[function]['remaining'] -= 1
我的问题是,如何处理函数的参数?我不确定如何准确地解释可能存在数量可变的参数,并且可能会命名一些参数。例如,c
的函数定义可能是:
def c(param1,param2=2, param3=3):
return param1 + param2 + param3
但是我可能只需要用param1
和param3
调用它
我有正确的总体方法吗?这感觉像是我可以用**
操作符来完成的事情,但我一直在思考如何准确地进行操作。编写一个decorator,并使用splat操作符来处理任意参数
例如:
def pause_wrapper(x, n):
def decorator(f):
config = [x, time.time()+n]
def wrapped(*args, **kwargs):
if config[0] == 0:
time.sleep(config[1] - time.time())
config = [x, time.time() + n]
return f(*args, **kwargs)
return wrapped
return decorator
使用方法:
@pause_wrapper(x, n)
def function(a, b, c):
...
*args
和**kwargs
非正式地称为“splat”参数。接受*args、**kwargs
的函数接收元组args
中的所有位置参数以及字典kwargs
中的所有关键字参数。(除了splats,您还可以有其他参数,在这种情况下,splats会吸收所有未发送到命名参数的参数)
传递*args
和**kwargs
具有相反的效果,传递args
的内容作为额外的位置参数,传递kwargs
作为关键字参数
使用这两种方法可以处理任何一组参数,无论是输入还是输出,都可以进行透明包装(如本例所示)。编写装饰器,并使用splat运算符处理任意参数
例如:
def pause_wrapper(x, n):
def decorator(f):
config = [x, time.time()+n]
def wrapped(*args, **kwargs):
if config[0] == 0:
time.sleep(config[1] - time.time())
config = [x, time.time() + n]
return f(*args, **kwargs)
return wrapped
return decorator
使用方法:
@pause_wrapper(x, n)
def function(a, b, c):
...
*args
和**kwargs
非正式地称为“splat”参数。接受*args、**kwargs
的函数接收元组args
中的所有位置参数以及字典kwargs
中的所有关键字参数。(除了splats,您还可以有其他参数,在这种情况下,splats会吸收所有未发送到命名参数的参数)
传递*args
和**kwargs
具有相反的效果,传递args
的内容作为额外的位置参数,传递kwargs
作为关键字参数
使用这两种方法可以处理任何一组参数,无论是输入还是输出,都可以进行透明包装(如本例所示)。这就是装饰器的基本用途
from collections import defaultdict
class counted:
calls = defaultdict(int)
def __init__(self,x,n):
self.x = x
self.n = n
def __call__(self,fn,*args,**kwargs):
results = fn(*args,**kwargs)
calls[fn.__name__] += 1
#do something with the count ...
@counted(3,9)
def functionWhatever(arg1,arg2,arg3,**kwargs):
return "55"
functionWhatever(1,2,3,something=5)
这就是装饰师的基本用途
from collections import defaultdict
class counted:
calls = defaultdict(int)
def __init__(self,x,n):
self.x = x
self.n = n
def __call__(self,fn,*args,**kwargs):
results = fn(*args,**kwargs)
calls[fn.__name__] += 1
#do something with the count ...
@counted(3,9)
def functionWhatever(arg1,arg2,arg3,**kwargs):
return "55"
functionWhatever(1,2,3,something=5)
^我认为这个答案是正确的。。。(基本上和我的一样,但我使用了一个类…)太好了,谢谢!我必须对装饰师做更多的研究,但这是朝着正确方向的一个很好的推动。^这个答案在我看来是正确的。。。(基本上和我的一样,但我使用了一个类…)太好了,谢谢!我必须对装饰器做更多的研究,但这是朝着正确方向的一个很好的推动。等等,装饰器上的
\uuuu new\uuuu
是如何工作的?很确定应该是\uuuu call\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
?等等,装饰师的\uuuuuuuuuuuuuu?很确定它应该是\uuuuuuuuuuuuuuuu
?