问题描述
我需要能够在该方法中获取当前执行的方法的名称
。我知道方法对象确实有
__name__属性,但我知道如何使用
方法访问它。
这样的事情:
A级:
.....
def a_method(自我,这个,那个):
打印< __ name __>
a = A()
a.a_method()
''a_method''
Thanx为你提供帮助!
-
Mitko Haralanov
高级软件工程师650.934.8064
系统互连组
I need to be able to get the name of the currently executed method
within that method. I know that the method object does have the
__name__ attribute but I know know how to access it from withing the
method.
Something like this:
class A:
.....
def a_method (self, this, that):
print <__name__>
a = A ()
a.a_method()
''a_method''
Thanx for your help!
--
Mitko Haralanov mi***@qlogic.com
Senior Software Engineer 650.934.8064
System Interconnect Group http://www.qlogic.com
推荐答案
我不确定这是你想要的,这对我来说似乎是多余的所以我是
可能误会了什么,但我没有;
I''m not sure this is what you want, it seems redundant to me so i am
probably misunderstanding something, but I did;
.... def a_method(self,this,that):
.... print self .a_method .__ name__
....
.... def a_method(self,this,that):
.... print self.a_method.__name__
....
a_method
a_method
因为你知道你在方法中,你可以对它进行硬编码:
def a_method(self,this,than ):
print" a_method"
除非
,否则无法从访问变量中获得任何收益打算用其他方法来打印。
-Larry
Since you KNOW you are in the method, you can hardcode it:
def a_method(self, this, than):
print "a_method"
There''s nothing to be gained from accessing a variable unless
you are going to be calling some other method to do the printing.
-Larry
... def a_method(self,this,that):
... print self.a_method .__ name__
... def a_method(self,this,that):
... print self.a_method.__name__
执行上述操作显然会有效!
但是,我不想使用该函数的名称
打印语句(.a_method。部分)。想象一下,在函数中有大约100个
以上的打印语句,然后你改变了函数的名字
。我希望所有100个打印语句都能正常工作,而不必更改每个打印语句以反映新的功能名称。
-
Mitko Haralanov
高级软件工程师650.934.8064
系统互连组
Doing the above will obviously work!
However, I don''t want to have to use the name of the function in the
print statement (the ".a_method." part). Imagine having about 100 of
the above print statements in the function and then you change the name
of the function. I want all 100 of the print statements to work without
having to change every one of them to reflect the new function name.
--
Mitko Haralanov mi***@qlogic.com
Senior Software Engineer 650.934.8064
System Interconnect Group http://www.qlogic.com
这篇关于从类方法中获取方法名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!