你可以使用內(nèi)建的dir函數(shù)來列出模塊定義的標(biāo)識(shí)符。標(biāo)識(shí)符有函數(shù)、類和變量。
當(dāng)你為dir()提供一個(gè)模塊名的時(shí)候,它返回模塊定義的名稱列表。如果不提供參數(shù),它返回當(dāng)
前模塊中定義的名稱列表。
$ python
>>> import sys
>>> dir(sys) # get list of attributes for sys module
['__displayhook__''__doc__''__excepthook__''__name__''__stderr__',
'__stdin__''__stdout__''_getframe''api_version''argv',
'builtin_module_names''byteorder''call_tracing''callstats',
'copyright''displayhook''exc_clear''exc_info''exc_type',
'excepthook''exec_prefix''executable''exit''getcheckinterval',
'getdefaultencoding''getdlopenflags''getfilesystemencoding',
'getrecursionlimit''getrefcount''hexversion''maxint''maxunicode',
'meta_path','modules''path''path_hooks''path_importer_cache',
'platform''prefix''ps1''ps2''setcheckinterval''setdlopenflags',
'setprofile''setrecursionlimit''settrace''stderr''stdin''stdout',
'version''version_info''warnoptions']
>>> dir() # get list of attributes for current module
['__builtins__''__doc__''__name__''sys']
>>>
>>> a = 5 # create a new variable 'a'
>>> dir()
[
'__builtins__''__doc__''__name__''a''sys']
>>>
>>> del a # delete/remove a name
>>>
>>> dir()
[
'__builtins__''__doc__''__name__''sys']