我想通过类中的方法进行迭代,或者根据目前的方法不同地处理类或实例对象。我如何获得类方法的列表?

还看到:

方法中的方法如何列出 Python 2.5模块? 循环在 Python / IronPython对象 方法 找到方法 对象有 我怎么看里面 Python对象? 我该怎么做 中对对象进行内省 Python 2. x ? 如何获得 对象的方法和的完整列表 属性? 找出 函数可以从类中获得 实例在python中?


当前回答

如果你的方法是“常规”方法,而不是静态方法、类方法等。 我想出了一个小窍门

for k, v in your_class.__dict__.items():
    if "function" in str(v):
        print(k)

这可以通过相应改变if条件中的“function”扩展到其他类型的方法。 在Python 2.7和Python 3.5中测试。

其他回答

你也可以从types中导入FunctionType并使用类测试它。__dict__:

from types import FunctionType

class Foo:
    def bar(self): pass
    def baz(self): pass

def methods(cls):
    return [x for x, y in cls.__dict__.items() if type(y) == FunctionType]

methods(Foo)  # ['bar', 'baz']

For my use case, I needed to distinguish between class methods, static methods, properties, and instance methods. The inspect module confuses the issue a bit (particularly with class methods and instance methods), so I used vars based on a comment on this SO question. The basic gist is to use vars to get the __dict__ attribute of the class, then filter based on various isinstance checks. For instance methods, I check that it is callable and not a class method. One caveat: this approach of using vars (or __dict__ for that matter) won't work with __slots__. Using Python 3.6.9 (because it's what the Docker image I'm using as my interpreter has):

class MethodAnalyzer:

    class_under_test = None

    @classmethod
    def get_static_methods(cls):
        if cls.class_under_test:
            return {
                k for k, v in vars(cls.class_under_test).items()
                if isinstance(v, staticmethod)
            }
        return {}

    @classmethod
    def get_class_methods(cls):
        if cls.class_under_test:
            return {
                k for k, v in vars(cls.class_under_test).items()
                if isinstance(v, classmethod)
            }
        return {}

    @classmethod
    def get_instance_methods(cls):
        if cls.class_under_test:
            return {
                k for k, v in vars(cls.class_under_test).items()
                if callable(v) and not isinstance(v, classmethod)
            }
        return {}

    @classmethod
    def get_properties(cls):
        if cls.class_under_test:
            return {
                k for k, v in vars(cls.class_under_test).items()
                if isinstance(v, property)
            }
        return {}

为了查看它的运行情况,我创建了这个小测试类:

class Foo:

    @staticmethod
    def bar(baz):
        print(baz)

    @property
    def bleep(self):
        return 'bloop'

    @classmethod
    def bork(cls):
        return cls.__name__

    def flank(self):
        return 'on your six'

那时:

MethodAnalyzer.class_under_test = Foo
print(MethodAnalyzer.get_instance_methods())
print(MethodAnalyzer.get_class_methods())
print(MethodAnalyzer.get_static_methods())
print(MethodAnalyzer.get_properties())

的输出

{'flank'}
{'bork'}
{'bar'}
{'bleep'}

在这个例子中,我放弃了实际的方法,但如果你需要保留它们,你可以使用字典理解式而不是集合理解式:

{
    k, v for k, v in vars(cls.class_under_test).items()
    if callable(v) and not isinstance(v, classmethod)
}
def find_defining_class(obj, meth_name):
    for ty in type(obj).mro():
        if meth_name in ty.__dict__:
            return ty

So

print find_defining_class(car, 'speedometer') 

Python第210页

注意,您需要考虑是否希望结果中包含继承(但不重写)基类的方法。dir()和inspect.getmembers()操作包含基类方法,但使用__dict__属性不包含。

你可以使用我创建的函数。

def method_finder(classname):

    non_magic_class = []

    class_methods = dir(classname)

    for m in class_methods:

        if m.startswith('__'):

            continue

        else:

            non_magic_class.append(m)

    return non_magic_class




method_finder(list)

输出:

['append',
 'clear',
 'copy',
 'count',
 'extend',
 'index',
 'insert',
 'pop',
 'remove',
 'reverse',
 'sort']