在Python中,如何获取成员函数的类的名称? [英] In Python, how can you get the name of a member function's class?

查看:200
本文介绍了在Python中,如何获取成员函数的类的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,该函数需要另一个函数作为参数.如果该函数是类的成员,则需要查找该类的名称.例如

I have a function that takes another function as a parameter. If the function is a member of a class, I need to find the name of that class. E.g.

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

我认为

testFunc.__class__ 

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

would solve my problems, but that just tells me that testFunc is a function.

推荐答案

testFunc.im_class

https://docs.python.org/reference /datamodel.html#the-standard-type-hierarchy

im_classim_self的类别,用于 绑定方法或要求的类 对于未绑定方法的方法

im_class is the class of im_self for bound methods or the class that asked for the method for unbound methods

这篇关于在Python中,如何获取成员函数的类的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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