Python基础学习篇—dir()函数
来源:互联网 发布:js cookie 编辑:程序博客网 时间:2024/05/22 11:16
您可以使用内置的dir()函数列出一个定义对象的标识符。例如,对于一个模块,包括在模块中定义的函数,类和变量。
当你给dir()提供一个模块名字时,它返回在那个模块中定义的名字的列表。当没有为其提供参数时, 它返回当前模块中定义的名字的列表。
dir() 函数举例:
>>> import sys # 获得属性列表,在这里是sys模块的属性列表
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_compact_freelists','_current_frames', '_getframe', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable','exit', 'flags', 'float_info', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof','gettrace', 'getwindowsversion', 'hexversion', 'intern', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions', 'winver']
>>> dir() # 获得当前模块的属性列表
['__builtins__', '__doc__', '__name__', '__package__', 'sys']
>>> a = 5 # 创建了一个新变量 'a'
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'a', 'sys']
>>> del a # 删除/移除一个名字
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'sys']
>>>
它是如何工作的:
首先,我们看到在导入sys模块上使用使用dir。我们能看到模块包含的巨大的属性列表。
然后,我们使用没有传递参数的dir函数。默认情况下,它返回模块的属性的列表。注意,导入模块的列表仍然是这个列表的一部分。
为了看到 dir在起作用,我们定义了一个新的变量,并为其赋值,然后检查dir,我们发现列表中添加了一个同名变量。我们使用del语句移除当前模块的变量或属性,在 del函数的输出中变化再次得到体现。
关于del的一点注意事项--这个语句用于删除一个变量/属性,语句运行后,这里是del a,你不能再访问变量a--就像它从来根本没有存在过。
注意,dir()函数对任何对象都起作用。例如,运行dir('print')来学习print函数的属性的更多知识,或运行dir(str)学习str类的属性的更多知识。
当你给dir()提供一个模块名字时,它返回在那个模块中定义的名字的列表。当没有为其提供参数时, 它返回当前模块中定义的名字的列表。
dir() 函数举例:
>>> import sys # 获得属性列表,在这里是sys模块的属性列表
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_compact_freelists','_current_frames', '_getframe', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable','exit', 'flags', 'float_info', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof','gettrace', 'getwindowsversion', 'hexversion', 'intern', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions', 'winver']
>>> dir() # 获得当前模块的属性列表
['__builtins__', '__doc__', '__name__', '__package__', 'sys']
>>> a = 5 # 创建了一个新变量 'a'
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'a', 'sys']
>>> del a # 删除/移除一个名字
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'sys']
>>>
它是如何工作的:
首先,我们看到在导入sys模块上使用使用dir。我们能看到模块包含的巨大的属性列表。
然后,我们使用没有传递参数的dir函数。默认情况下,它返回模块的属性的列表。注意,导入模块的列表仍然是这个列表的一部分。
为了看到 dir在起作用,我们定义了一个新的变量,并为其赋值,然后检查dir,我们发现列表中添加了一个同名变量。我们使用del语句移除当前模块的变量或属性,在 del函数的输出中变化再次得到体现。
关于del的一点注意事项--这个语句用于删除一个变量/属性,语句运行后,这里是del a,你不能再访问变量a--就像它从来根本没有存在过。
注意,dir()函数对任何对象都起作用。例如,运行dir('print')来学习print函数的属性的更多知识,或运行dir(str)学习str类的属性的更多知识。
0 0
- Python基础学习篇—dir()函数
- python dir函数
- python dir()函数作用
- python函数 - dir()
- 4.5 Python dir()函数
- python-dir()函数
- python dir()函数
- Python的dir函数
- Python 内置函数 dir()
- python: dir函数
- Python的dir()函数
- 学习python的第三十一天-模块,dir()函数
- python基础-dir()和__dict__
- python内建函数 dir()
- python dir()内建函数
- Python函数---dir(),help(),type()
- Python基础(二、代码调试方式dir,help,print函数,pycharm破解教程)
- python学习(7)——函数基础篇
- C++中,malloc/free和new/delete的区别
- android attr到theme的内在关系
- STL 之 stack
- FileOutputStream和FileInputStream使用方法说明
- GCD笔记
- Python基础学习篇—dir()函数
- bcm 端口
- 提示并转向
- Linux中gmtime和localtime的区别
- 翼支付门户架构之redis高可用部署及监控
- ubuntu创建桌面启动器的方法
- 获取Android状态栏高度
- linux下解压命令大全
- QT拖拽功能简介