Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算未知Python函数中的内部调用_Python_Function_Count - Fatal编程技术网

计算未知Python函数中的内部调用

计算未知Python函数中的内部调用,python,function,count,Python,Function,Count,我正在尝试编写一个函数,可以计算未知函数调用子函数的次数。count函数将使用未知函数及其参数(args、kwargs)作为参数,调用函数(parameter),然后递归返回函数返回值以及函数内部和所有子调用函数内部的函数调用次数。对函数本身的调用也不计算在内 如果我的话没有什么意义,下面是一个例子: def count_calls(function, *args, **kwargs): #This is the function that I try to write pass def ad

我正在尝试编写一个函数,可以计算未知函数调用子函数的次数。count函数将使用未知函数及其参数(args、kwargs)作为参数,调用函数(parameter),然后递归返回函数返回值以及函数内部和所有子调用函数内部的函数调用次数。对函数本身的调用也不计算在内

如果我的话没有什么意义,下面是一个例子:

def count_calls(function, *args, **kwargs):
#This is the function that I try to write
pass

def add(a, b):
    return a + b

def add_hundred(a):
    return add(a, 100)
运行功能时的预期结果:
count\u调用(add,8,12)-(0,20)
0是子函数调用发生的次数(因为对函数本身的调用不被计数,所以这里为零;20是
add(8,12)
count\u调用(add\u-hund,5)-(1105)
as
add\u-hund
将进行一个子函数调用
add

我做了一些研究,但找不到任何内置的库或函数可以做到这一点或帮助这一点


谢谢。

sys.settrace()
将是您的出发点。@jasonharper噢,谢谢!现在就来看看吧。