我如何知道在Python中执行函数的位置?

我如何知道在Python中执行函数的位置?,python,function,introspection,Python,Function,Introspection,让我们想象一下,我有一个模块foo.py,其中声明了一个函数foofunc: def foofunc(): # smart things return 和两个不同的模块-bar.py和spam.py,其中存在直接从foo模块执行功能的代码 # `bar` module. All rights reserved. from foo import foofunc def run_foofunc(): return foofunc() 在另一个模块中也是如此: #

让我们想象一下,我有一个模块
foo.py
,其中声明了一个函数
foofunc

def foofunc():
    # smart things
    return
和两个不同的模块-
bar.py
spam.py
,其中存在直接从
foo
模块执行功能的代码

# `bar` module. All rights reserved.

from foo import foofunc


def run_foofunc():
     return foofunc()
在另一个模块中也是如此:

# `spam` module. All rights reserved.

from foo import foofunc


def run_foofunc():
    return foofunc()
我需要知道函数在哪里执行,而不知道可能的位置。比如:

def foofunc():
    print inspect.executedfrom()
将在标准输出中执行类似的操作

<pack.bar.run_foofunc>


它在现实世界中是否类似?

冒着不回答实际问题的风险,您写道您需要它来进行研究和调试

我认为这个模块非常适合这一点

import traceback
traceback.print_stack()

还可以看看,它允许您在运行时以交互方式逐步浏览代码。

冒着不回答实际问题的风险,您写道您需要它来进行研究和调试

我认为这个模块非常适合这一点

import traceback
traceback.print_stack()

另请看一看,它允许您在运行时以交互方式逐步浏览代码。

test.py:

import inspect    

def foofunc():
    frame, filename, line_number, function_name, lines, index = inspect.getouterframes(
        inspect.currentframe())[1]

    print(filename, line_number, function_name)
    return
import test
def run_foofunc():
    test.foofunc()

run_foofunc()

foo.py:

import inspect    

def foofunc():
    frame, filename, line_number, function_name, lines, index = inspect.getouterframes(
        inspect.currentframe())[1]

    print(filename, line_number, function_name)
    return
import test
def run_foofunc():
    test.foofunc()

run_foofunc()
屈服

('/tmp/foo.py', 3, 'run_foofunc')

test.py:

import inspect    

def foofunc():
    frame, filename, line_number, function_name, lines, index = inspect.getouterframes(
        inspect.currentframe())[1]

    print(filename, line_number, function_name)
    return
import test
def run_foofunc():
    test.foofunc()

run_foofunc()

foo.py:

import inspect    

def foofunc():
    frame, filename, line_number, function_name, lines, index = inspect.getouterframes(
        inspect.currentframe())[1]

    print(filename, line_number, function_name)
    return
import test
def run_foofunc():
    test.foofunc()

run_foofunc()
屈服

('/tmp/foo.py', 3, 'run_foofunc')

以下是从执行的
的可能实现:

import inspect

def executed_from():
    f = inspect.currentframe().f_back.f_back
    return "%s.%s" % (inspect.getmodule(f).__name__, f.f_code.co_name)

以下是从
执行的
的可能实现:

import inspect

def executed_from():
    f = inspect.currentframe().f_back.f_back
    return "%s.%s" % (inspect.getmodule(f).__name__, f.f_code.co_name)

在等待你的答案的过程中,我找到了自己的答案。通过在已调试函数中引发异常并逐层进行回溯,可以解决此问题。这种方法通常是不好的,因为它会停止执行,但在我的特殊情况下并不坏。除此之外,它很容易清洗。

在等待你的答案的过程中,我找到了自己的答案。通过在已调试函数中引发异常并逐层进行回溯,可以解决此问题。这种方法通常是不好的,因为它会停止执行,但在我的特殊情况下并不坏。除此之外,它很容易清洁。

不管实际答案是什么,似乎你做得不对。你为什么要这么做?我在一个名为OpenStack的大型项目中工作;)我是新来的。我需要它的研究和调试。可能的重复无论实际答案是什么,似乎你做得不对。你为什么要这么做?我在一个名为OpenStack的大型项目中工作;)我是新来的。我需要它进行研究和调试。可能的重复我会投票给你,因为这是正确的答案,但请不要在生产代码中使用它。@Daantimer在我看来,如果函数根据调用方的不同表现不同,这是错误代码布局的标志,应该参数化。这样,您就可以轻松地在其他地方重复使用该函数,对其进行单元测试,并且在移动调用方时不必触摸该函数。此外,使用
inspect
模块会对PyPy等Python解释器的速度产生影响。我会投票给你,因为这是正确的答案,但请不要在生产代码中使用它。@Daantimer在我看来,如果函数的行为因调用方而异,这是错误代码布局的标志,应该参数化。这样,您就可以轻松地在其他地方重复使用该函数,对其进行单元测试,并且在移动调用方时不必触摸该函数。此外,使用
inspect
模块对PyPy等Python解释器有速度影响。您不必引发异常,例如,使用
traceback
模块并将输出保存到文件中。您不必引发异常,例如,使用
traceback
模块并将输出保存到文件中。