从函数内部访问func_name [英] Accessing func_name from inside a function

查看:129
本文介绍了从函数内部访问func_name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从函数内部访问函数的名称。我的第一个想法是不行的。

def foo():
.... print func_name

.... foo()
Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,在?

文件"< stdin>",第2行,在foo中

NameError:全局名称''func_name''没有定义


我的第二次尝试有效,但看起来很难看。

def foo():
.... import inspect

.... print inspect.stack()[0] [3]

.... foo()



foo


有没有从内部获取函数名称的标准方法

函数?

解决方案

James Thiele写道:

是否有一种从内部获取函数名称的标准方法
函数?



不,没有。


Martin


James Thiele写道:

我想从函数内部访问函数的名称。



http://aspn.activestate.com/ASPN/Coo...n/Recipe/66062


肯特


好的。但这和我的尝试一样丑陋。


I''d like to access the name of a function from inside the function. My
first idea didn''t work.

def foo(): .... print func_name
.... foo() Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in foo
NameError: global name ''func_name'' is not defined

My second attempt works but looks ugly.
def foo(): .... import inspect
.... print inspect.stack()[0][3]
.... foo()


foo

Is there a standard way of getting the name of a function from inside
the function?

解决方案

James Thiele wrote:

Is there a standard way of getting the name of a function from inside
the function?



No, there isn''t.

Martin


James Thiele wrote:

I''d like to access the name of a function from inside the function.



http://aspn.activestate.com/ASPN/Coo...n/Recipe/66062

Kent


OK. But that''s just as ugly as my attempt.


这篇关于从函数内部访问func_name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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