Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.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 嵌套函数中的cProfile_Python_Cprofile - Fatal编程技术网

Python 嵌套函数中的cProfile

Python 嵌套函数中的cProfile,python,cprofile,Python,Cprofile,我正在尝试使用cProfile.run分析嵌套函数。我知道可能cProfile的运行范围与我调用它的范围不同,但我不太确定实现这一点的惯用方法是什么。这是一个MVCE: def foo(): def bar(): # do something here return 1 cProfile.run('bar()') 给出了错误: NameError: name 'bar' is not defined 使用: 使用cProfile.run def

我正在尝试使用
cProfile.run
分析嵌套函数。我知道可能
cProfile
的运行范围与我调用它的范围不同,但我不太确定实现这一点的惯用方法是什么。这是一个MVCE:

def foo():
    def bar():
        # do something here
        return 1
    cProfile.run('bar()')
给出了错误:

NameError: name 'bar' is not defined
使用:

使用cProfile.run

def foo():
    def bar():
        # do something here
        return 1
    cProfile.run(bar.__code__)
def foo():
    def bar():
        # do something here
        return 1
    cProfile.run(bar.__code__)