li is a list, so dir(li) returns a list of all the methods of a list. Note that the returned list contains the names of the methods as strings, not the methods themselves.
根据评论进行编辑:
不,这也将显示所有继承的方法。举个例子:
测试.py:
class Foo:
def foo(): pass
class Bar(Foo):
def bar(): pass
Python解释器:
>>> from test import Foo, Bar
>>> dir(Foo)
['__doc__', '__module__', 'foo']
>>> dir(Bar)
['__doc__', '__module__', 'bar', 'foo']
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. For
example, metaclass attributes are not
in the result list when the argument
is a class.
你想看看^{} 函数:
根据评论进行编辑:
不,这也将显示所有继承的方法。举个例子:
测试.py:
Python解释器:
您应该注意到Python's documentation状态:
因此在代码中使用是不安全的。改用
vars()
。Vars()
不包括有关超类的信息,您必须自己收集它们。如果使用
dir()
在交互式解释器中查找信息,请考虑使用help()
。为了更便于阅读,您可以使用see:
dir()和vars()不适合你吗?
相关问题 更多 >
编程相关推荐