2014-01-23 66 views
1

好做,我知道DIR()函數,但我得到這一切如何找出哪些模塊蟒蛇

>>> dir(sys) 
['__displayhook__', '__doc__', '__excepthook__', '__loader__', '__name__', '__package__', '__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', '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'] 

我不知道任何這些事情。我聽到的幫助()函數,但它不爲我工作,或者當我加入「」它不給我任何信息

>>> help(path) 
Traceback (most recent call last): 
    File "<pyshell#22>", line 1, in <module> 
    help(path) 
NameError: name 'path' is not defined 

>>> help('path') 
no Python documentation found for 'path' 

回答

3

你叫help正是你打電話dir以同樣的方式。因此:

>>> import sys 
>>> help(sys) 

Help on built-in module sys: 

NAME 
    sys 

FILE 
    (built-in) 

MODULE DOCS 
    http://docs.python.org/library/sys 

DESCRIPTION 
    This module provides access to some objects used or maintained by the 
    interpreter and to functions that interact strongly with the interpreter. 
... (lot of text follows) 

由於沒有模塊也不是符號路徑help(path)什麼都找不到。並且添加引號將無助於任何事情。但是:

>>> help(sys.path) 

Help on list object: 

class list(object) 
| list() -> new empty list 
... (lot of text follows) 

但請注意,它打印的幫助類上的傳遞的對象,list的,不能對變量。變量不是python中的第一類對象,所以命令無法發現參數來自sys.path,只能在模塊中找到它的幫助。函數有附加到函數對象的幫助,所以help確實爲您傳遞的特定函數打印幫助。

+0

啊完美,第二個代碼正是我所需要的。謝謝 – Atlas

1

除了通過help()提供的內置文檔,請不要忘記excellent online documentation。您可以查看module index(Python 2鏈接),也可以查看python sys.path之類的鏈接,通常第一個鏈接會幫您找到正確的鏈接。一旦你在那裏,你可以通過左上角的下拉菜單選擇你正在使用的確切版本。