【问题标题】:how to get the pointer to the current function in python? [duplicate]如何在python中获取指向当前函数的指针? [复制]
【发布时间】:2012-08-29 04:42:32
【问题描述】:

可能重复:
Python code to get current function into a variable?

有没有方便的方法来获取当前函数的指针?

例如:

current_func_ref = None
def func():
    current_func_ref = ___ #something need to fill
    do something..

【问题讨论】:

  • 您想要引用堆栈上正在调用的当前函数吗?
  • 你到底想做什么?这对 Python 来说是一种非常奇怪的模式。
  • 注意:current_func_ref 在函数内部创建一个 local 名称。它对外部级别的current_func_ref 没有影响。

标签: python function


【解决方案1】:

好吧,这有点厚颜无耻,不像你想要的那样通用,但无论如何:

current_func_ref = None
def func():
    global current_func_ref
    current_func_ref = func

【讨论】:

  • 更改函数名时,也需要更改代码。
  • @user1544915 是的,因此我将其描述为厚脸皮而不是通用的。但就像其他评论者所说的那样,即使是一开始就想做这件事也很奇怪。你真正想解决什么问题?
猜你喜欢
  • 2011-03-04
  • 1970-01-01
  • 1970-01-01
  • 2012-09-18
  • 2022-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-09
相关资源
最近更新 更多