【发布时间】:2010-09-07 06:14:07
【问题描述】:
给定一个任何类型的 Python 对象,是否有一种简单的方法可以获取该对象具有的所有方法的列表?
或者,
如果这不可能,除了简单地检查调用方法时是否发生错误之外,是否至少有一种简单的方法可以检查它是否具有特定的方法?
【问题讨论】:
-
您可以使用 dir(object) 将所有这些都放在一个列表中
标签: python introspection
给定一个任何类型的 Python 对象,是否有一种简单的方法可以获取该对象具有的所有方法的列表?
或者,
如果这不可能,除了简单地检查调用方法时是否发生错误之外,是否至少有一种简单的方法可以检查它是否具有特定的方法?
【问题讨论】:
标签: python introspection
对于许多对象,您可以使用此代码,将“object”替换为您感兴趣的对象:
object_methods = [method_name for method_name in dir(object)
if callable(getattr(object, method_name))]
我在diveintopython.net 发现了它(现已存档)。希望这应该提供更多细节!
如果您收到AttributeError,则可以改用它:
getattr( 不能容忍 pandas 风格的 python3.6 抽象虚拟子类。这段代码和上面一样,忽略异常。
import pandas as pd
df = pd.DataFrame([[10, 20, 30], [100, 200, 300]],
columns=['foo', 'bar', 'baz'])
def get_methods(object, spacing=20):
methodList = []
for method_name in dir(object):
try:
if callable(getattr(object, method_name)):
methodList.append(str(method_name))
except:
methodList.append(str(method_name))
processFunc = (lambda s: ' '.join(s.split())) or (lambda s: s)
for method in methodList:
try:
print(str(method.ljust(spacing)) + ' ' +
processFunc(str(getattr(object, method).__doc__)[0:90]))
except:
print(method.ljust(spacing) + ' ' + ' getattr() failed')
get_methods(df['foo'])
【讨论】:
print [method for method in dir(object) if callable(getattr(object, method))].
AttributeError: module 'pandas.core.common' has no attribute 'AbstractMethodError'。在stackoverflow.com/q/54713287/9677043查看详细信息。
您可以使用内置的dir() 函数来获取模块具有的所有属性的列表。在命令行中尝试一下,看看它是如何工作的。
>>> import moduleName
>>> dir(moduleName)
此外,您可以使用hasattr(module_name, "attr_name") 函数来确定模块是否具有特定属性。
请参阅Guide to Python introspection 了解更多信息。
【讨论】:
hasattr 帮助我的用例查找 python 对象是否具有特定的成员变量或方法。
'attrib_name' in dir(moduleName) 来查找模块是否具有特定属性。
最简单的方法是使用dir(objectname)。它将显示该对象可用的所有方法。很酷的把戏。
【讨论】:
AttributeError: module 'pandas.core.common' has no attribute 'AbstractMethodError'。有任何想法吗?请参阅stackoverflow.com/q/54713287/9677043 的 deets。 +1 给@Pawan Kumar b/c 答案是有效的,并给@ljs 以保证只包含方法的过滤列表。
我相信你想要这样的东西:
对象的属性列表
内置函数dir()可以完成这项工作。
取自 Python shell 上的 help(dir) 输出:
目录(...)
dir([object]) -> list of strings如果在没有参数的情况下调用,则返回当前范围内的名称。
否则,返回一个按字母顺序排列的名称列表,其中包括给定对象的(部分)属性以及可从该对象访问的属性。
如果对象提供了一个名为
__dir__的方法,它将被使用;否则 使用默认的 dir() 逻辑并返回:
- 对于模块对象:模块的属性。
- 对于一个类对象:它的属性,以及它的基类的递归属性。
- 对于任何其他对象:其属性、其类的属性以及递归其类的基类的属性。
例如:
$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = "I am a string"
>>>
>>> type(a)
<class 'str'>
>>>
>>> dir(a)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',
'__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__',
'__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__',
'__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'_formatter_field_name_split', '_formatter_parser', 'capitalize',
'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find',
'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace',
'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition',
'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip',
'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title',
'translate', 'upper', 'zfill']
【讨论】:
检查它是否有特定的方法:
hasattr(object,"method")
【讨论】:
if hasattr(obj,method) and callable(getattr(obj,method)):
获取任何对象的方法列表的最简单方法是使用help() 命令。
help(object)
它将列出与该对象关联的所有可用/重要方法。
例如:
help(str)
【讨论】:
% 在第一个示例中做了什么?它在我的 Python 2.7 中不起作用。
如果你特别想要方法,你应该使用inspect.ismethod。
对于方法名称:
import inspect
method_names = [attr for attr in dir(self) if inspect.ismethod(getattr(self, attr))]
对于方法本身:
import inspect
methods = [member for member in [getattr(self, attr) for attr in dir(self)] if inspect.ismethod(member)]
有时inspect.isroutine 也很有用(对于内置、C 扩展、没有“绑定”编译器指令的 Cython)。
【讨论】:
inspect.getmembers而不是dir吗?
inspect.getmembers(self, predicate=inspect.ismethod) ?
假设我们有一个 Python obj。然后看看它所有的方法,包括__(magic methods)包围的方法:
print(dir(obj))
要排除内置的魔法,可以这样做:
[m for m in dir(obj) if not m.startswith('__')]
【讨论】:
打开一个 Bash shell(在 Ubuntu 上是 Ctrl + Alt + T)。在其中启动 Python 3 shell。创建一个对象来观察方法。只需在其后添加一个点,然后按两次 Tab,您会看到如下内容:
user@note:~$ python3
Python 3.4.3 (default, Nov 17 2016, 01:08:31)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import readline
>>> readline.parse_and_bind("tab: complete")
>>> s = "Any object. Now it's a string"
>>> s. # here tab should be pressed twice
s.__add__( s.__rmod__( s.istitle(
s.__class__( s.__rmul__( s.isupper(
s.__contains__( s.__setattr__( s.join(
s.__delattr__( s.__sizeof__( s.ljust(
s.__dir__( s.__str__( s.lower(
s.__doc__ s.__subclasshook__( s.lstrip(
s.__eq__( s.capitalize( s.maketrans(
s.__format__( s.casefold( s.partition(
s.__ge__( s.center( s.replace(
s.__getattribute__( s.count( s.rfind(
s.__getitem__( s.encode( s.rindex(
s.__getnewargs__( s.endswith( s.rjust(
s.__gt__( s.expandtabs( s.rpartition(
s.__hash__( s.find( s.rsplit(
s.__init__( s.format( s.rstrip(
s.__iter__( s.format_map( s.split(
s.__le__( s.index( s.splitlines(
s.__len__( s.isalnum( s.startswith(
s.__lt__( s.isalpha( s.strip(
s.__mod__( s.isdecimal( s.swapcase(
s.__mul__( s.isdigit( s.title(
s.__ne__( s.isidentifier( s.translate(
s.__new__( s.islower( s.upper(
s.__reduce__( s.isnumeric( s.zfill(
s.__reduce_ex__( s.isprintable(
s.__repr__( s.isspace(
【讨论】:
ipython,开始输入对象并按tab,它也可以正常工作。无需读取行设置
这里指出的所有方法的问题是您不能确定某个方法不存在。
在 Python 中,您可以通过__getattr__ 和__getattribute__ 拦截点调用,从而可以“在运行时”创建方法
例子:
class MoreMethod(object):
def some_method(self, x):
return x
def __getattr__(self, *args):
return lambda x: x*2
如果执行的话,可以调用对象字典中不存在的方法...
>>> o = MoreMethod()
>>> o.some_method(5)
5
>>> dir(o)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattr__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'some_method']
>>> o.i_dont_care_of_the_name(5)
10
这就是您在 Python 中使用 Easier to ask for forgiveness than permission 范式的原因。
【讨论】:
没有可靠的方法来列出所有对象的方法。 dir(object) 通常很有用,但在某些情况下它可能不会列出所有方法。根据dir() documentation:“使用参数,尝试返回该对象的有效属性列表。”
检查方法是否存在可以由callable(getattr(object, method)) 完成,正如那里已经提到的那样。
【讨论】:
import moduleName
for x in dir(moduleName):
print(x)
这应该工作:)
【讨论】:
可以创建一个getAttrs 函数,该函数将返回对象的可调用属性名称
def getAttrs(object):
return filter(lambda m: callable(getattr(object, m)), dir(object))
print getAttrs('Foo bar'.split(' '))
那会回来的
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',
'__delslice__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__getitem__', '__getslice__', '__gt__', '__iadd__', '__imul__', '__init__',
'__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__',
'__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__',
'__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop',
'remove', 'reverse', 'sort']
【讨论】:
以列表为对象
obj = []
list(filter(lambda x:callable(getattr(obj,x)),obj.__dir__()))
你得到:
['__add__',
'__class__',
'__contains__',
'__delattr__',
'__delitem__',
'__dir__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__gt__',
'__iadd__',
'__imul__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__reversed__',
'__rmul__',
'__setattr__',
'__setitem__',
'__sizeof__',
'__str__',
'__subclasshook__',
'append',
'clear',
'copy',
'count',
'extend',
'index',
'insert',
'pop',
'remove',
'reverse',
'sort']
【讨论】:
...除了简单地检查调用方法时是否发生错误之外,是否至少有一种简单的方法可以检查它是否具有特定的方法
虽然“Easier to ask for forgiveness than permission”当然是 Pythonic 方式,但您可能正在寻找:
d={'foo':'bar', 'spam':'eggs'}
if 'get' in dir(d):
d.get('foo')
# OUT: 'bar'
【讨论】:
我已经完成了以下函数(get_object_functions),它接收一个对象(object_)作为其参数,返回一个包含所有方法(包括静态和类)的列表(functions)方法)在对象的类中定义:
def get_object_functions(object_):
functions = [attr_name
for attr_name in dir(object_)
if str(type(getattr(object_,
attr_name))) in ("<class 'function'>",
"<class 'method'>")]
return functions
好吧,它只是检查类属性类型的字符串表示是否等于"<class 'function'>" 或"<class 'method'>",然后如果是True,则将该属性包含在functions 列表中。
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print(f'My name is {self.name}')
@staticmethod
def say_hi():
print('hi')
@classmethod
def reproduce(cls, name):
return cls(name, 0)
person = Person('Rafael', 27)
print(get_object_functions(person))
['__init__', 'introduce', 'reproduce', 'say_hi']
对于更简洁的代码版本: https://github.com/revliscano/utilities/blob/master/get_object_functions/object_functions_getter.py
【讨论】:
为了在整个模块中搜索特定的方法
for method in dir(module) :
if "keyword_of_methode" in method :
print(method, end="\n")
【讨论】:
例如,如果您正在使用 shell plus,则可以改用它:
>> MyObject??
那样,用 '??'就在您的对象之后,它会向您显示该类具有的所有属性/方法。
【讨论】:
您可以使用 Python 中预定义的 dir()。
import module_name
dir(module_name)
您也可以将对象传递给 dir()
dir(object_name)
如果对象是预定义类的对象,例如 int、str 等。它会显示其中的方法(您可能知道这些方法是内置函数)。如果该对象是为用户定义的类创建的,它会显示该类中给出的所有方法。
【讨论】:
这是一个不错的衬线(但也会获得属性):
print(*dir(obj), sep='\n')
【讨论】:
大多数时候,我希望看到用户定义的方法,而不希望看到以'__'开头的内置属性,如果您愿意,可以使用以下代码:
object_methods = [method_name for method_name in dir(object) if callable(getattr(object, method_name)) and '__' not in method_name]
例如,对于这个类:
class Person:
def __init__(self, name):
self.name = name
def print_name(self):
print(self.name)
上面的代码将打印:['print_name']
【讨论】: