[英]How to use any function by a decorator regardless of its arguments in Python
I'm looking to create a function that will return how long it takes to execute any other function (maybe it can be done with a library but in principle I'm looking to implement it natively).我正在寻找一个函数,它将返回执行任何其他函数所需的时间(也许它可以用一个库来完成,但原则上我希望在本地实现它)。 I am looking to implement it in a native way), a priori it seems easy and it could be done either with decorators or with a classic function that receives a function by parameter:
我希望以本机方式实现它),先验它似乎很容易,它可以使用装饰器或通过参数接收函数的经典函数来完成:
def get_function_time(function):
start_time = time.time()
function()
return time.time() - start_time
Of course, this only would work with functions without parameters, in that case we could tell it to pass the arguments for the function:当然,这只适用于没有参数的函数,在这种情况下,我们可以告诉它为函数传递参数:
def get_function_time(function, parameter):
start_time = time.time()
function(parameter)
return time.time() - start_time
Here we would have the same problem when calling functions with more or less parameters than one.在这里,当调用参数多于或少于一个的函数时,我们会遇到同样的问题。 Then my doubt comes here, is there a way with decorators or without them that according to the number of elements I send the same number of argmuments in the function.
然后我的疑问就来了,有没有一种方法可以使用装饰器或没有装饰器,根据元素的数量我在函数中发送相同数量的参数。
For some tests that I needed to do I implemented this crappy solution that taking into account that I didn't have functions with more than 5 argmuents.对于我需要做的一些测试,我实现了这个糟糕的解决方案,考虑到我没有超过 5 个参数的函数。 functions of more than 5 argmuentos served me.
超过 5 个 argmuentos 的功能为我服务。
def get_function_time(function, parameters: list):
start = time.time()
parameters_num = len(parameters)
if len(parameters) > 6: raise ValueError("Max 5 arguments")
if parameters_num == 0:
function()
elif parameters_num == 1:
function(parameters[0])
elif parameters_num == 2:
function(parameters[0], parameters[1])
elif parameters_num == 3:
function(parameters[0], parameters[1], parameters[2])
elif parameters_num == 4:
function(parameters[0], parameters[1], parameters[2], parameters[3])
elif parameters_num == 5:
function(parameters[0], parameters[1], parameters[2], parameters[3], parameters[4])
return time.time() - start
But I was left with the question of how to do it in a dynamic way being able to measure any function no matter its arguments (as long as they are passed their arguments well, of course).但是我留下了一个问题,即如何以动态的方式来测量任何函数,不管它的参数是什么(当然,只要它们很好地传递了它们的参数)。
Sorry if the way of writing the question is not the best, I don't have much experience around here.抱歉,如果写问题的方式不是最好的,我在这里没有太多经验。
If I get it right, what you need is *args
and **kwargs
, which means arguments by orders and arguments by keywords.如果我猜对了,您需要的是
*args
和**kwargs
,这意味着按顺序的参数和按关键字的参数。
For example例如
def get_function_time(function, *args, **kwargs):
start = time.time()
function(*args, **kwargs)
return time.time() - start
then you can use it like this那么你可以像这样使用它
get_function_time(function, 'abc', 'def', keyword1='ghi', keyword2='jkl')
this is equivalent to calling function
as这相当于调用
function
function('abc', 'def', keyword1='ghi', keyword2='jkl')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.