使用内建的dir
函数来列出模块定义的标识符。标识符有函数、类和变量。
>>> dir() #获取当前模块的属性
['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
>>> import sys #输入sys模块
>>> dir(sys) #显示sys模块的属性
['__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_getframe', '_home', '_mercurial', '_xoptions', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'getwindowsversion', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'last_traceback', 'last_type', 'last_value', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info', 'warnoptions', 'winver']
>>> str = 'Hello python world!' #将字符串变量str加入当前模块
>>> dir() #获取当前模块的属性
['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'str', 'sys']
>>> print(str) #输出当前模块中的str
Hello python world!
>>> del str #删除当前模块中的str变量
>>> del sys #删除模块sys
>>> dir() #显示当前模块的属性
['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
>>>