从该函数中确定函数名称(不使用追溯) [英] Determine function name from within that function (without using traceback)

查看:116
本文介绍了从该函数中确定函数名称(不使用追溯)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,如果不使用 traceback 模块,有没有一种方法可以从该函数中确定函数的名称?



说我有一个功能栏foo模块。在执行 foo.bar()时,有没有一种方法让酒吧知道酒吧的名字?或者更好的是, foo.bar 的名字?

  #foo .py 
def bar():
printmy name is,__myname__#< ==如何在运行时计算这个值?


解决方案

Python没有访问函数的功能或者它在函数本身的名字。它已被提出,但遭到拒绝。如果你不想自己玩堆叠,你应该使用bar bar .__ name __ 取决于上下文。



给出的拒绝通知是:

lockquote

这个PEP是拒绝。目前尚不清楚应该如何实施,或者边缘案例中的精确语义应该如何,并且没有给出足够的重要用例。反应最好不过了。


In Python, without using the traceback module, is there a way to determine a function's name from within that function?

Say I have a module foo with a function bar. When executing foo.bar(), is there a way for bar to know bar's name? Or better yet, foo.bar's name?

#foo.py  
def bar():
    print "my name is", __myname__ # <== how do I calculate this at runtime?

解决方案

Python doesn't have a feature to access the function or its name within the function itself. It has been proposed but rejected. If you don't want to play with the stack yourself, you should either use "bar" or bar.__name__ depending on context.

The given rejection notice is:

This PEP is rejected. It is not clear how it should be implemented or what the precise semantics should be in edge cases, and there aren't enough important use cases given. response has been lukewarm at best.

这篇关于从该函数中确定函数名称(不使用追溯)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆