在Python中的另一个函数中获取调用函数名称?

时间:2022-05-10 20:42:46

If you have 2 functions like:

如果你有两个功能,如:

def A
def B

and A calls B, can you get who is calling B inside B, like:

和A打电话给B,你能得到谁在B里面叫B,比如:

def A () :
    B ()

def B () :
    this.caller.name

5 个解决方案

#1


108  

You can use the inspect module to get the calling stack. It returns a list of frame records. The third element in each record is the caller name. What you want is this:

您可以使用inspect模块获取调用堆栈。它返回一个帧记录列表。每条记录中的第三个元素是调用者名称。你想要的是这个:

>>> import inspect
>>> def f():
...     print inspect.stack()[1][3]
...
>>> def g():
...     f()
...
>>> g()
g

Of course, it is a good idea to check that enough frame records exist before trying to access a particular index.

当然,在尝试访问特定索引之前检查是否存在足够的帧记录是个好主意。

#2


14  

There are two ways, using sys and inspect modules:

有两种方法,使用sys和inspect模块:

  • sys._getframe(1).f_code.co_name
  • sys._getframe(1).f_code.co_name
  • inspect.stack()[1][3]
  • inspect.stack()[1] [3]

The stack() form is less readable and is implementation dependent since it calls sys._getframe(), see extract from inspect.py:

stack()形式的可读性较差,并且依赖于实现,因为它调用sys._getframe(),请参阅inspect.py中的extract:

def stack(context=1):
    """Return a list of records for the stack above the caller's frame."""
    return getouterframes(sys._getframe(1), context)

#3


9  

Note (June 2018): today, I would probably use inspect module, see other answers

注意(2018年6月):今天,我可能会使用检查模块,请参阅其他答案

sys._getframe(1).f_code.co_name like in the example below:

sys._getframe(1).f_code.co_name,如下例所示:

>>> def foo():
...  global x
...  x = sys._getframe(1)
...
>>> def y(): foo()
...
>>> y()
>>> x.f_code.co_name
'y'
>>>  

Important note: as it's obvious from the _getframe method name (hey, it starts with an underscore), it's not an API method one should be thoughtlessly rely on.

重要说明:从_getframe方法名称中可以明显看出(嘿,它以下划线开头),它不是一种应该不加思索地依赖的API方法。

#4


6  

This works for me! :D

这适合我! :d

>>> def a():
...     import sys
...     print sys._getframe(1).f_code.co_name
...
>>> def b():
...     a()
...
...
>>> b()
b
>>>

#5


2  

you can user the logging module and specify the %(filename)s option in BaseConfig()

您可以使用日志记录模块并在BaseConfig()中指定%(filename)选项

import logging
logging.basicConfig(filename='/tmp/test.log', level=logging.DEBUG, format='%(asctime)s | %(levelname)s | %(funcName)s |%(message)s')

def A():
    logging.info('info')

#1


108  

You can use the inspect module to get the calling stack. It returns a list of frame records. The third element in each record is the caller name. What you want is this:

您可以使用inspect模块获取调用堆栈。它返回一个帧记录列表。每条记录中的第三个元素是调用者名称。你想要的是这个:

>>> import inspect
>>> def f():
...     print inspect.stack()[1][3]
...
>>> def g():
...     f()
...
>>> g()
g

Of course, it is a good idea to check that enough frame records exist before trying to access a particular index.

当然,在尝试访问特定索引之前检查是否存在足够的帧记录是个好主意。

#2


14  

There are two ways, using sys and inspect modules:

有两种方法,使用sys和inspect模块:

  • sys._getframe(1).f_code.co_name
  • sys._getframe(1).f_code.co_name
  • inspect.stack()[1][3]
  • inspect.stack()[1] [3]

The stack() form is less readable and is implementation dependent since it calls sys._getframe(), see extract from inspect.py:

stack()形式的可读性较差,并且依赖于实现,因为它调用sys._getframe(),请参阅inspect.py中的extract:

def stack(context=1):
    """Return a list of records for the stack above the caller's frame."""
    return getouterframes(sys._getframe(1), context)

#3


9  

Note (June 2018): today, I would probably use inspect module, see other answers

注意(2018年6月):今天,我可能会使用检查模块,请参阅其他答案

sys._getframe(1).f_code.co_name like in the example below:

sys._getframe(1).f_code.co_name,如下例所示:

>>> def foo():
...  global x
...  x = sys._getframe(1)
...
>>> def y(): foo()
...
>>> y()
>>> x.f_code.co_name
'y'
>>>  

Important note: as it's obvious from the _getframe method name (hey, it starts with an underscore), it's not an API method one should be thoughtlessly rely on.

重要说明:从_getframe方法名称中可以明显看出(嘿,它以下划线开头),它不是一种应该不加思索地依赖的API方法。

#4


6  

This works for me! :D

这适合我! :d

>>> def a():
...     import sys
...     print sys._getframe(1).f_code.co_name
...
>>> def b():
...     a()
...
...
>>> b()
b
>>>

#5


2  

you can user the logging module and specify the %(filename)s option in BaseConfig()

您可以使用日志记录模块并在BaseConfig()中指定%(filename)选项

import logging
logging.basicConfig(filename='/tmp/test.log', level=logging.DEBUG, format='%(asctime)s | %(levelname)s | %(funcName)s |%(message)s')

def A():
    logging.info('info')