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

还看到:

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


当前回答

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)
}

其他回答

一个示例(列出了optparse的方法)。OptionParser类):

>>> from optparse import OptionParser
>>> import inspect
#python2
>>> inspect.getmembers(OptionParser, predicate=inspect.ismethod)
[([('__init__', <unbound method OptionParser.__init__>),
...
 ('add_option', <unbound method OptionParser.add_option>),
 ('add_option_group', <unbound method OptionParser.add_option_group>),
 ('add_options', <unbound method OptionParser.add_options>),
 ('check_values', <unbound method OptionParser.check_values>),
 ('destroy', <unbound method OptionParser.destroy>),
 ('disable_interspersed_args',
  <unbound method OptionParser.disable_interspersed_args>),
 ('enable_interspersed_args',
  <unbound method OptionParser.enable_interspersed_args>),
 ('error', <unbound method OptionParser.error>),
 ('exit', <unbound method OptionParser.exit>),
 ('expand_prog_name', <unbound method OptionParser.expand_prog_name>),
 ...
 ]
# python3
>>> inspect.getmembers(OptionParser, predicate=inspect.isfunction)
...

注意,getmembers返回一个二元组列表。第一项是成员的名称,第二项是值。

你也可以传递一个实例给getmembers:

>>> parser = OptionParser()
>>> inspect.getmembers(parser, predicate=inspect.ismethod)
...

试试属性__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']

我只是把这个放在那里,因为排名靠前的答案不清楚。

这是一个简单的测试,不常用类基于Enum。

# -*- coding: utf-8 -*-
import sys, inspect
from enum import Enum

class my_enum(Enum):
    """Enum base class my_enum"""
    M_ONE = -1
    ZERO = 0
    ONE = 1
    TWO = 2
    THREE = 3

    def is_natural(self):
            return (self.value > 0)
    def is_negative(self):
            return (self.value < 0)

def is_clean_name(name):
    return not name.startswith('_') and not name.endswith('_')
def clean_names(lst):
    return [ n for n in lst if is_clean_name(n) ]
def get_items(cls,lst):
    try:
            res = [ getattr(cls,n) for n in lst ]
    except Exception as e:
            res = (Exception, type(e), e)
            pass
    return res


print( sys.version )

dir_res = clean_names( dir(my_enum) )
inspect_res = clean_names( [ x[0] for x in inspect.getmembers(my_enum) ] )
dict_res = clean_names( my_enum.__dict__.keys() )

print( '## names ##' )
print( dir_res )
print( inspect_res )
print( dict_res )

print( '## items ##' )
print( get_items(my_enum,dir_res) )
print( get_items(my_enum,inspect_res) )
print( get_items(my_enum,dict_res) )

这是输出结果。

3.7.7 (default, Mar 10 2020, 13:18:53) 
[GCC 9.2.1 20200306]
## names ##
['M_ONE', 'ONE', 'THREE', 'TWO', 'ZERO']
['M_ONE', 'ONE', 'THREE', 'TWO', 'ZERO', 'name', 'value']
['is_natural', 'is_negative', 'M_ONE', 'ZERO', 'ONE', 'TWO', 'THREE']
## items ##
[<my_enum.M_ONE: -1>, <my_enum.ONE: 1>, <my_enum.THREE: 3>, <my_enum.TWO: 2>, <my_enum.ZERO: 0>]
(<class 'Exception'>, <class 'AttributeError'>, AttributeError('name'))
[<function my_enum.is_natural at 0xb78a1fa4>, <function my_enum.is_negative at 0xb78ae854>, <my_enum.M_ONE: -1>, <my_enum.ZERO: 0>, <my_enum.ONE: 1>, <my_enum.TWO: 2>, <my_enum.THREE: 3>]

所以我们有:

Dir提供的数据不完整 检查。Getmembers提供不完整的数据,并提供getattr()无法访问的内部键。 __dict__.keys()提供完整可靠的结果

为什么投票如此错误?我错在哪里?其他答案的得票这么低的人哪里错了?

这只是一种观察。"encode"似乎是一个字符串对象的方法

str_1 = 'a'
str_1.encode('utf-8')
>>> b'a'

但是,如果在str1中检查方法,则返回一个空列表

inspect.getmember(str_1, predicate=inspect.ismethod)
>>> []

所以,也许我错了,但问题似乎并不简单。