简体   繁体   English

Python中是否有一个函数列出特定对象的属性和方法?

[英]Is there a function in Python to list the attributes and methods of a particular object?

Is there a function in Python to list the attributes and methods of a particular object? Python中是否有一个函数列出特定对象的属性和方法?

Something like: 就像是:

ShowAttributes ( myObject )

   -> .count
   -> .size

ShowMethods ( myObject )

   -> len
   -> parse

You want to look at the dir() function: 你想看看dir()函数:

>>> li = []
>>> dir(li)      
['append', 'count', 'extend', 'index', 'insert',
'pop', 'remove', 'reverse', 'sort']

li is a list, so dir(li) returns a list of all the methods of a list. li是一个列表,因此dir(li)返回列表中所有方法的列表。 Note that the returned list contains the names of the methods as strings, not the methods themselves. 请注意,返回的列表包含方法的名称作为字符串,而不是方法本身。


Edit in response to comment: 编辑以回应评论:

No this will show all inherited methods as well. 不,这也将显示所有继承的方法。 Consider this example: 考虑这个例子:

test.py: test.py:

class Foo:
    def foo(): pass

class Bar(Foo):
    def bar(): pass

Python interpreter: Python解释器:

>>> from test import Foo, Bar
>>> dir(Foo)
['__doc__', '__module__', 'foo']
>>> dir(Bar)
['__doc__', '__module__', 'bar', 'foo']

You should note that Python's documentation states: 您应该注意 Python的文档说明:

Note: Because dir() is supplied primarily as a convenience for use at an interactive prompt , it tries to supply an interesting set of names more than it tries to supply a rigorously or consistently defined set of names, and its detailed behavior may change across releases . 注意:因为dir() 主要是为了方便在交互式提示中使用而提供的 ,所以它尝试提供一组有趣的名称,而不是尝试提供严格或一致定义的名称集, 并且其详细行为可能会发生变化发布 For example, metaclass attributes are not in the result list when the argument is a class. 例如,当参数是类时,元类属性不在结果列表中。

Therefore it's not safe to use in your code. 因此,在代码中使用它是不安全的。 Use vars() instead. 请改用vars() Vars() doesn't include information about the superclasses, you'd have to collect them yourself. Vars()不包含有关超类的信息,您必须自己收集它们。


If you're using dir() to find information in an interactive interpreter, consider the use of help() . 如果您使用dir()在交互式解释器中查找信息,请考虑使用help()

dir()和vars()不适合你吗?

and for a more human-readable way, you can use see : 并且为了更人性化的方式,您可以使用see

In [1]: from see import see
In [2]: x = "hello world!"
In [3]: see(x)
Out[3]: 
  []   in   +   *   %   <   <=   ==   !=   >   >=   hash()   help()   len()
  repr()   str()   .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()

Another way to do this is with the nifty IPython environment. 另一种方法是使用漂亮的IPython环境。 It lets you tab complete to find all the methods and fields of an object. 它允许您完成选项卡以查找对象的所有方法和字段。

令我惊讶的是,没有人提到python对象函数:
keys()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM