请用通俗语言说一下在什么情况下经常用到,谢啦
dir() 函数
尽管查找和导入模块相对容易,但要记住每个模块包含什么却不是这么简单。您并不希望总是必须查看源代码来找出答案。幸运的是,Python 提供了一种方法,可以使用内置的 dir() 函数来检查模块(以及其它对象)的内容。
dir() 函数可能是 Python 自省机制中最著名的部分了。它返回传递给它的任何对象的属性名称经过排序的列表。如果不指定对象,则 dir() 返回当前作用域中的名称
尽管查找和导入模块相对容易,但要记住每个模块包含什么却不是这么简单。您并不希望总是必须查看源代码来找出答案。幸运的是,Python 提供了一种方法,可以使用内置的 dir() 函数来检查模块(以及其它对象)的内容。
dir() 函数可能是 Python 自省机制中最著名的部分了。它返回传递给它的任何对象的属性名称经过排序的列表。如果不指定对象,则 dir() 返回当前作用域中的名称
温馨提示:答案为网友推荐,仅供参考
第1个回答 2010-02-20
dir(...)
dir([object]) -> list of strings
Return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it:
No argument: the names in the current scope.
Module object: the module attributes.
Type or class object: its attributes, and recursively the attributes of
its bases.
Otherwise: its attributes, its class's attributes, and recursively the
attributes of its class's base classes.
dir([object]) -> list of strings
Return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it:
No argument: the names in the current scope.
Module object: the module attributes.
Type or class object: its attributes, and recursively the attributes of
its bases.
Otherwise: its attributes, its class's attributes, and recursively the
attributes of its class's base classes.
参考资料:python2.5
本回答被网友采纳第2个回答 2010-02-19
lst = dir(obj)
返回一个列表包含obj所包含变量的名字
如:
import os
print "\n".join(dir(os))
返回一个列表包含obj所包含变量的名字
如:
import os
print "\n".join(dir(os))
第3个回答 2010-03-01
记不起模块里面有什么函数,变量,嵌套的模块等,dir(obj)后可以找到。
第4个回答 2010-03-03
简单的说,就是它列出来对象里有哪些方法(记住python里任何东西都是对象)