在Python中,如何获取成员函数的名称';什么课?

在Python中,如何获取成员函数的名称';什么课?,python,reflection,metaprogramming,Python,Reflection,Metaprogramming,我有一个函数,它把另一个函数作为参数。如果函数是一个类的成员,我需要找到该类的名称。例如 def analyser(testFunc): print testFunc.__name__, 'belongs to the class, ... 我想 testFunc.__class__ 可以解决我的问题,但这只是告诉我testFunc是一个函数。我不是Python专家,但这行得通吗 testFunc.__self__.__class__ 它似乎适用于绑定方法,但在您的情况下,您可能

我有一个函数,它把另一个函数作为参数。如果函数是一个类的成员,我需要找到该类的名称。例如

def analyser(testFunc):
    print testFunc.__name__, 'belongs to the class, ...
我想

testFunc.__class__ 

可以解决我的问题,但这只是告诉我testFunc是一个函数。

我不是Python专家,但这行得通吗

testFunc.__self__.__class__
它似乎适用于绑定方法,但在您的情况下,您可能正在使用未绑定方法,在这种情况下,这可能会更好:

testFunc.__objclass__
以下是我使用的测试:

Python 2.5.2 (r252:60911, Jul 31 2008, 17:31:22) 
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hashlib
>>> hd = hashlib.md5().hexdigest
>>> hd
<built-in method hexdigest of _hashlib.HASH object at 0x7f9492d96960>
>>> hd.__self__.__class__
<type '_hashlib.HASH'>
>>> hd2 = hd.__self__.__class__.hexdigest
>>> hd2
<method 'hexdigest' of '_hashlib.HASH' objects>
>>> hd2.__objclass__
<type '_hashlib.HASH'>
Python 2.5.2(r252:60911,2008年7月31日,17:31:22)
linux2上的[GCC 4.2.3(Ubuntu 4.2.3-2ubuntu7)]
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>导入hashlib
>>>hd=hashlib.md5().hexdigest
>>>高清
>>>hd.\uuuuu self.\uuuuuuuu.\uuuuuuuu类__
>>>hd2=hd.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
>>>hd2
>>>hd2.对象类__
哦,是的,还有一件事:

>>> hd.im_class
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute 'im_class'
>>> hd2.im_class
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'method_descriptor' object has no attribute 'im_class'
>>hd.im\u类
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:“内置函数”或“方法”对象没有属性“im类”
>>>hd2.im_类
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:“方法描述符”对象没有属性“im类”
所以,如果你想要防弹的东西,它也应该处理
\uuuuujclass\uuuuuu
\uuuuuuu self\uuuuu
。但是你的里程数可能会有所不同

testFunc.im_class

im\u class
是用于 绑定方法或请求的类 对于未绑定方法的方法


实例方法将具有attributes.im\u class.im\u func.im\u self


您可能希望查看函数是否有attr.im\u类,并从中获取类信息。

从Python3.3中,
.im\u类
已消失。您可以改为使用
。\uuuuu qualname\uuuuu
。以下是相应的政治公众人物:

使用嵌套函数:

def f():
    def g():
        pass
    return g

f.__qualname__  # 'f'
f().__qualname__  # 'f.<locals>.g'
def():
def g():
通过
返回g
f、 _uuuqualname_uu#f'
f()

请使用以下函数获取类内部的方法名称

def getLocalMethods(clss):
import types
# This is a helper function for the test function below.
# It returns a sorted list of the names of the methods
# defined in a class. It's okay if you don't fully understand it!
result = [ ]
for var in clss.__dict__:
    val = clss.__dict__[var]
    if (isinstance(val, types.FunctionType)):
        result.append(var)
return sorted(result)

不,这会给您一条消息:“AttributeError:‘function’object没有‘self’属性。请尝试objclass属性,看看是否有效。提示:当您遇到这样的问题时,您可以在解释器中使用“dir”来查看testFunc有哪种方法,或者更好:ipython选项卡完成帮助!我的回答总是太迟了。我只是在新文档中找不到用户定义的方法节+1股;对于我给出的示例,您的解决方案非常有效。不幸的是,我把实际问题简化得太多了。我在另一个问题中提出了这一点:
def getLocalMethods(clss):
import types
# This is a helper function for the test function below.
# It returns a sorted list of the names of the methods
# defined in a class. It's okay if you don't fully understand it!
result = [ ]
for var in clss.__dict__:
    val = clss.__dict__[var]
    if (isinstance(val, types.FunctionType)):
        result.append(var)
return sorted(result)