我想通过类中的方法进行迭代,或者根据目前的方法不同地处理类或实例对象。我如何获得类方法的列表?
还看到:
方法中的方法如何列出 Python 2.5模块? 循环在 Python / IronPython对象 方法 找到方法 对象有 我怎么看里面 Python对象? 我该怎么做 中对对象进行内省 Python 2. x ? 如何获得 对象的方法和的完整列表 属性? 找出 函数可以从类中获得 实例在python中?
我想通过类中的方法进行迭代,或者根据目前的方法不同地处理类或实例对象。我如何获得类方法的列表?
还看到:
方法中的方法如何列出 Python 2.5模块? 循环在 Python / IronPython对象 方法 找到方法 对象有 我怎么看里面 Python对象? 我该怎么做 中对对象进行内省 Python 2. x ? 如何获得 对象的方法和的完整列表 属性? 找出 函数可以从类中获得 实例在python中?
当前回答
如果你只想列出一个python类的方法
import numpy as np
print(np.random.__all__)
其他回答
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页
有这样一种方法:
[getattr(obj, m) for m in dir(obj) if not m.startswith('__')]
在处理类实例时,也许返回一个包含方法引用的列表,而不是仅仅返回名称¹会更好。如果这是你的目标,还有
使用不导入 从列表中排除私有方法(例如__init__)
这可能会有用。你可能还想确保它是可调用的(getattr(obj, m)),因为dir返回obj中的所有属性,而不仅仅是方法。
简而言之,对于一个班级来说
class Ghost:
def boo(self, who):
return f'Who you gonna call? {who}'
我们可以检查实例检索
>>> g = Ghost()
>>> methods = [getattr(g, m) for m in dir(g) if not m.startswith('__')]
>>> print(methods)
[<bound method Ghost.boo of <__main__.Ghost object at ...>>]
所以你可以马上调用它:
>>> for method in methods:
... print(method('GHOSTBUSTERS'))
...
Who you gonna call? GHOSTBUSTERS
¹一个用例:
我使用它进行单元测试。有一个类,其中所有方法都执行相同过程的变体——这导致了冗长的测试,每个方法与其他方法之间只有细微的差别。DRY是一个遥远的梦想。
我想我应该有一个单一的测试所有的方法,所以我做了上面的迭代。
尽管我意识到我应该重构代码本身,无论如何都是符合dry的…这可能在未来仍然服务于一个随机的挑剔的灵魂。
以上这些方法对我都没用。
我在编写pytests时遇到过这个问题。
我找到的唯一解决办法是:
1-创建另一个目录,把我所有的。py文件放在那里
2-为我的pytests创建一个单独的目录,然后导入我感兴趣的类
这允许我在类中获得最新的方法-您可以更改方法名称,然后使用print(dir(class))来确认它。
你可以使用我创建的函数。
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']
我知道这是一篇旧文章,但只是写了这个函数,并将它留在这里,以防有人跌跌撞撞地寻找答案:
def classMethods(the_class,class_only=False,instance_only=False,exclude_internal=True):
def acceptMethod(tup):
#internal function that analyzes the tuples returned by getmembers tup[1] is the
#actual member object
is_method = inspect.ismethod(tup[1])
if is_method:
bound_to = tup[1].im_self
internal = tup[1].im_func.func_name[:2] == '__' and tup[1].im_func.func_name[-2:] == '__'
if internal and exclude_internal:
include = False
else:
include = (bound_to == the_class and not instance_only) or (bound_to == None and not class_only)
else:
include = False
return include
#uses filter to return results according to internal function and arguments
return filter(acceptMethod,inspect.getmembers(the_class))