最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Get caller's function name from a class - Stack Overflow

programmeradmin0浏览0评论

I'm trying to create a class variable and assign the name of the function calling that class, which is being imported from a separate .py. But nothing seems to be working.

File 1 (caller)

from file2 import A

def func():  # <-- Function name I want to get
    a = A()  # Initializing class A

File 2 (where class A is)

class A:
    
    func_name = ...  # <-- Where I would like to get the caller function name (func)
    
    def __init__(self):
        ...

I've tried:

  • inspect.stack()[1][3]
  • inspect.currentframe().f_back.f_code.co_name
  • inspect.getframeinfo(sys._getframe(1))[2]
  • sys._getframe(1).f_code.co_name

But so far all I'm getting is either: <module> or <frozen importlib._bootstrap>, so I'm wondering if it's possible to get the caller's function name if the class is being imported in a separate file.

I'm trying to create a class variable and assign the name of the function calling that class, which is being imported from a separate .py. But nothing seems to be working.

File 1 (caller)

from file2 import A

def func():  # <-- Function name I want to get
    a = A()  # Initializing class A

File 2 (where class A is)

class A:
    
    func_name = ...  # <-- Where I would like to get the caller function name (func)
    
    def __init__(self):
        ...

I've tried:

  • inspect.stack()[1][3]
  • inspect.currentframe().f_back.f_code.co_name
  • inspect.getframeinfo(sys._getframe(1))[2]
  • sys._getframe(1).f_code.co_name

But so far all I'm getting is either: <module> or <frozen importlib._bootstrap>, so I'm wondering if it's possible to get the caller's function name if the class is being imported in a separate file.

Share Improve this question asked Feb 6 at 8:03 YoRHa_A2YoRHa_A2 431 silver badge6 bronze badges New contributor YoRHa_A2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 6
  • 2 FWIW, except for debugging purposes, you should hardly ever do this for any reason. – deceze Commented Feb 6 at 8:06
  • 1 Also, you need to do the stack trace in __init__, not in the class definition…!? – deceze Commented Feb 6 at 8:07
  • Also, you should show exactly what you tried along with actual and desired results, including tracebacks, if any. Make a minimal reproducible example. – Mark Tolonen Commented Feb 6 at 8:11
  • 2 This shouts XY problem to me. What use would knowing the calling function be to the class? If it relies on that information to change behaviour, something has gone wrong with the setup – roganjosh Commented Feb 6 at 8:11
  • Your attempts would work if you did them in __init__ rather than in the class definition, where the caller is the import system. – blhsing Commented Feb 6 at 8:18
 |  Show 1 more comment

1 Answer 1

Reset to default 1

Try this at file2 (where class A is):

class A:
    def __init__(self):
        import inspect
        __f = inspect.getframeinfo(inspect.currentframe().f_back)
        __func = __f.function
        self.func_name = __func
发布评论

评论列表(0)

  1. 暂无评论