问题:查找Python对象具有的方法
给定任何种类的Python对象,是否有一种简单的方法来获取该对象具有的所有方法的列表?
要么,
如果这不可能,那么除了简单地检查调用该方法时是否发生错误之外,是否至少有一种简单的方法来检查它是否具有特定的方法?
Given a Python object of any kind, is there an easy way to get the list of all methods that this object has?
Or,
if this is not possible, is there at least an easy way to check if it has a particular method other than simply checking if an error occurs when the method is called?
回答 0
对于许多对象,您可以使用以下代码,将“ object”替换为您感兴趣的对象:
object_methods = [method_name for method_name in dir(object)
if callable(getattr(object, method_name))]
我在diveintopython.net上发现了它(现已存档)。希望可以提供更多详细信息!
如果得到AttributeError
,则可以改用:
getattr(
不能容忍熊猫风格的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'])
For many objects, you can use this code, replacing ‘object’ with the object you’re interested in:
object_methods = [method_name for method_name in dir(object)
if callable(getattr(object, method_name))]
I discovered it at diveintopython.net (Now archived). Hopefully, that should provide some further detail!
If you get an AttributeError
, you can use this instead:
getattr(
is intolerant of pandas style python3.6 abstract virtual sub-classes. This code does the same as above and ignores exceptions.
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'])
回答 1
您可以使用内置dir()
函数来获取模块具有的所有属性的列表。在命令行上尝试此操作以查看其工作原理。
>>> import moduleName
>>> dir(moduleName)
另外,您可以使用 hasattr(module_name, "attr_name")
函数来查找模块是否具有特定属性。
有关更多信息,请参见Python自省指南。
You can use the built in dir()
function to get a list of all the attributes a module has. Try this at the command line to see how it works.
>>> import moduleName
>>> dir(moduleName)
Also, you can use the hasattr(module_name, "attr_name")
function to find out if a module has a specific attribute.
See the Guide to Python introspection for more information.
回答 2
最简单的方法是使用dir(objectname)
。它将显示该对象可用的所有方法。很酷的把戏。
The simplest method is to use dir(objectname)
. It will display all the methods available for that object. Cool trick.
回答 3
要检查它是否具有特定方法:
hasattr(object,"method")
To check if it has a particular method:
hasattr(object,"method")
回答 4
我相信您想要的是这样的:
来自对象的属性列表
以我的拙见,内置功能dir()
可以为您完成这项工作。取自help(dir)
Python Shell上的输出:
目录(…)
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']
在检查您的问题时,我决定展示我的思路,并以更好的格式输出 dir()
。
dir_attributes.py(Python 2.7.6)
#!/usr/bin/python
""" Demonstrates the usage of dir(), with better output. """
__author__ = "ivanleoncz"
obj = "I am a string."
count = 0
print "\nObject Data: %s" % obj
print "Object Type: %s\n" % type(obj)
for method in dir(obj):
# the comma at the end of the print, makes it printing
# in the same line, 4 times (count)
print "| {0: <20}".format(method),
count += 1
if count == 4:
count = 0
print
dir_attributes.py(Python 3.4.3)
#!/usr/bin/python3
""" Demonstrates the usage of dir(), with better output. """
__author__ = "ivanleoncz"
obj = "I am a string."
count = 0
print("\nObject Data: ", obj)
print("Object Type: ", type(obj),"\n")
for method in dir(obj):
# the end=" " at the end of the print statement,
# makes it printing in the same line, 4 times (count)
print("| {:20}".format(method), end=" ")
count += 1
if count == 4:
count = 0
print("")
希望我有所贡献:)。
I believe that what you want is something like this:
a list of attributes from an object
In my humble opinion, the built-in function dir()
can do this job for you. Taken from help(dir)
output on your Python Shell:
dir(…)
dir([object]) -> list of strings
If called without an argument, return the names in the current scope.
Else, return an alphabetized list of names comprising (some of) the attributes of the given object, and of attributes reachable from it.
If the object supplies a method named __dir__
, it will be used; otherwise
the default dir() logic is used and returns:
- for a module object: the module’s attributes.
- for a class object: its attributes, and recursively the attributes of its bases.
- for any other object: its attributes, its class’s attributes, and recursively the attributes of its class’s base classes.
For example:
$ 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']
As I was checking your issue, I decided to demonstrate my train of thought, with a better formatting of the output of dir()
.
dir_attributes.py (Python 2.7.6)
#!/usr/bin/python
""" Demonstrates the usage of dir(), with better output. """
__author__ = "ivanleoncz"
obj = "I am a string."
count = 0
print "\nObject Data: %s" % obj
print "Object Type: %s\n" % type(obj)
for method in dir(obj):
# the comma at the end of the print, makes it printing
# in the same line, 4 times (count)
print "| {0: <20}".format(method),
count += 1
if count == 4:
count = 0
print
dir_attributes.py (Python 3.4.3)
#!/usr/bin/python3
""" Demonstrates the usage of dir(), with better output. """
__author__ = "ivanleoncz"
obj = "I am a string."
count = 0
print("\nObject Data: ", obj)
print("Object Type: ", type(obj),"\n")
for method in dir(obj):
# the end=" " at the end of the print statement,
# makes it printing in the same line, 4 times (count)
print("| {:20}".format(method), end=" ")
count += 1
if count == 4:
count = 0
print("")
Hope that I have contributed :).
回答 5
除了更直接的答案外,如果我不提及iPython,我也会不知所措。点击“选项卡”以查看具有自动补全功能的可用方法。
找到方法后,请尝试:
help(object.method)
查看pydocs,方法签名等。
啊… REPL。
On top of the more direct answers, I’d be remiss if I didn’t mention iPython. Hit ‘tab’ to see the available methods, with autocompletion.
And once you’ve found a method, try:
help(object.method)
to see the pydocs, method signature, etc.
Ahh… REPL.
回答 6
如果您特别想要方法,则应使用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)。
If you specifically want methods, you should use inspect.ismethod.
For method names:
import inspect
method_names = [attr for attr in dir(self) if inspect.ismethod(getattr(self, attr))]
For the methods themselves:
import inspect
methods = [member for member in [getattr(self, attr) for attr in dir(self)] if inspect.ismethod(member)]
Sometimes inspect.isroutine
can be useful too (for built-ins, C extensions, Cython without the “binding” compiler directive).
回答 7
打开bash shell(在Ubuntu上为ctrl + alt + T)。在其中启动python3 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(
Open bash shell (ctrl+alt+T on Ubuntu). Start python3 shell in it. Create object to observe methods of. Just add a dot after it and press twice “tab” and you’ll see something like that:
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(
回答 8
此处指出的所有方法的问题在于,您不能确定某个方法不存在。
在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而不是权限范式来请求宽恕。
The problem with all methods indicated here is that you CAN’T be sure that a method doesn’t exist.
In Python you can intercept the dot calling thru __getattr__
and __getattribute__
, making it possible to create method “at runtime”
Exemple:
class MoreMethod(object):
def some_method(self, x):
return x
def __getattr__(self, *args):
return lambda x: x*2
If you execute it, you can call method non existing in the object dictionary…
>>> 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
And it’s why you use the Easier to ask for forgiveness than permission paradigms in Python.
回答 9
获取任何对象的方法列表的最简单方法是使用help()
命令。
%help(object)
它将列出与该对象关联的所有可用/重要方法。
例如:
help(str)
The simplest way to get list of methods of any object is to use help()
command.
%help(object)
It will list out all the available/important methods associated with that object.
For example:
help(str)
回答 10
import moduleName
for x in dir(moduleName):
print(x)
这应该工作:)
import moduleName
for x in dir(moduleName):
print(x)
This should work :)
回答 11
可以创建一个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']
One can create a getAttrs
function that will return an object’s callable property names
def getAttrs(object):
return filter(lambda m: callable(getattr(object, m)), dir(object))
print getAttrs('Foo bar'.split(' '))
That’d return
['__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']
回答 12
没有可靠的方法来列出所有对象的方法。dir(object)
通常是有用的,但是在某些情况下,它可能不会列出所有方法。根据dir()
文档:“使用参数尝试返回该对象的有效属性列表。”
callable(getattr(object, method))
如此处已经提到的,可以检查该方法是否存在。
There is no reliable way to list all object’s methods. dir(object)
is usually useful, but in some cases it may not list all methods. According to dir()
documentation: “With an argument, attempt to return a list of valid attributes for that object.”
Checking that method exists can be done by callable(getattr(object, method))
as already mentioned there.
回答 13
以清单为对象
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']
Take a list as an object
obj = []
list(filter(lambda x:callable(getattr(obj,x)),obj.__dir__()))
You get:
['__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']
回答 14
…至少有一种简单的方法可以检查它是否具有特定的方法,而不仅仅是在调用该方法时检查是否发生错误
虽然“ 比请求更容易获得宽恕 ”无疑是Python方式,但您正在寻找的可能是:
d={'foo':'bar', 'spam':'eggs'}
if 'get' in dir(d):
d.get('foo')
# OUT: 'bar'
…is there at least an easy way to check if it has a particular method other than simply checking if an error occurs when the method is called
While “Easier to ask for forgiveness than permission” is certainly the Pythonic way, what you are looking for maybe:
d={'foo':'bar', 'spam':'eggs'}
if 'get' in dir(d):
d.get('foo')
# OUT: 'bar'
回答 15
为了在整个模块中搜索特定方法
for method in dir(module) :
if "keyword_of_methode" in method :
print(method, end="\n")
In order to search for a specific method in a whole module
for method in dir(module) :
if "keyword_of_methode" in method :
print(method, end="\n")
回答 16
例如,如果您使用的是shell plus,则可以改用以下命令:
>> MyObject??
这样,用“ ??” 在对象之后,它将向您显示该类具有的所有属性/方法。
If you are, for instance, using shell plus you can use this instead:
>> MyObject??
that way, with the ‘??’ just after your object, it’ll show you all the attributes/methods the class has.
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。