1. 程式人生 > >【學習Python】Python檢視幫助---help函式

【學習Python】Python檢視幫助---help函式

help函式是python的一個內建函式(python的內建函式可以直接呼叫,無需import),它是python自帶的函式,任何時候都可以被使用。help函式能作什麼、怎麼使用help函式檢視python模組中函式的用法,和使用help函式時需要注意哪些問題,下面來簡單的說一下。

一、help()函式的作用
在使用python來編寫程式碼時,會經常使用python自帶函式或模組,一些不常用的函式或是模組的用途不是很清楚,這時候就需要用到help函式來檢視幫助。
這裡要注意下,help()函式是檢視函式或模組用途的詳細說明,而dir()函式是檢視函式或模組內的操作方法都有什麼,輸出的是方法列表。
二、怎麼使用help函式檢視python模組中函式的用法


help()括號內填寫引數,操作方法很簡單。例如:

複製程式碼 程式碼如下: >>> help('dir')
Help on built-in function dir in module builtins:

dir(...)
    dir([object]) -> list of strings

    If called without an argument, return the names in the current scope.
    Else, return an alphabetized list of names comprising (some of) the attribut
es
    of the given object, and of attributes reachable from it.
    If the object supplies a method named __dir__, it will be used; otherwise
    the default dir() logic is used and returns:
      for a module object: the module's attributes.
      for a class object:  its attributes, and recursively the attributes
        of its bases.
      for any other object: its attributes, its class's attributes, and
        recursively the attributes of its class's base classes.

三、使用help函式檢視幫助例項

在寫help()函式使用方法時說過,括號中填寫引數,那在這裡要注意引數的形式:

1、檢視一個模組的幫助

複製程式碼 程式碼如下: >>>help('sys')
之後它回開啟這個模組的幫助文件
2、檢視一個數據型別的幫助
複製程式碼 程式碼如下: >>>help('str')
返回字串的方法及詳細說明
複製程式碼 程式碼如下: >>>a = [1,2,3]
>>>help(a)
這時help(a)則會開啟list的操作方法
複製程式碼 程式碼如下: >>>help(a.append)

會顯示list的append方法的幫助

檢視python所有的modules:help("modules")

單看python所有的modules中包含指定字串的modules: help("modules yourstr")

檢視python中常見的topics: help("topics")

檢視python標準庫中的module:import os.path + help("os.path")

檢視python內建的型別:help("list")

檢視python型別的成員方法:help("str.find") 

檢視python內建函式:help("open")

檢視所有的關鍵字:help("keywords")


檢視所有的modules:help("modules")


單看所有的modules中包含指定字串的modules: help("modules yourstr")


檢視中常見的topics: help("topics")

>>> help("topics")

Here is a list of available topics.  Enter any topic name to get more help.

ASSERTION           DEBUGGING           LITERALS            SEQUENCEMETHODS2
ASSIGNMENT          DELETION            LOOPING             SEQUENCES
ATTRIBUTEMETHODS    DICTIONARIES        MAPPINGMETHODS      SHIFTING
ATTRIBUTES          DICTIONARYLITERALS  MAPPINGS            SLICINGS
AUGMENTEDASSIGNMENT DYNAMICFEATURES     METHODS             SPECIALATTRIBUTES
BACKQUOTES          ELLIPSIS            MODULES             SPECIALIDENTIFIERS
BASICMETHODS        EXCEPTIONS          NAMESPACES          SPECIALMETHODS
BINARY              EXECUTION           NONE                STRINGMETHODS
BITWISE             EXPRESSIONS         NUMBERMETHODS       STRINGS
BOOLEAN             FILES               NUMBERS             SUBSCRIPTS
CALLABLEMETHODS     FLOAT               OBJECTS             TRACEBACKS
CALLS               FORMATTING          OPERATORS           TRUTHVALUE
CLASSES             FRAMEOBJECTS        PACKAGES            TUPLELITERALS
CODEOBJECTS         FRAMES              POWER               TUPLES
COERCIONS           FUNCTIONS           PRECEDENCE          TYPEOBJECTS
COMPARISON          IDENTIFIERS         PRINTING            TYPES
COMPLEX             IMPORTING           PRIVATENAMES        UNARY
CONDITIONAL         INTEGER             RETURNING           UNICODE
CONTEXTMANAGERS     LISTLITERALS        SCOPING             
CONVERSIONS         LISTS               SEQUENCEMETHODS1    
例如想要檢視CALLS主題的幫助,則:help("CALLS")


檢視標準庫中的module:import os.path + help("os.path")
檢視內建的型別:help("list")
檢視型別的成員方法:help("str.find") 
檢視內建函式:help("open")

描述

help() 函式用於檢視函式或模組用途的詳細說明。

語法

help 語法:

help([object])

引數說明:

  • object -- 物件;

返回值

返回物件幫助資訊。

例項

以下例項展示了 help 的使用方法:

>>>help('sys')# 檢視 sys 模組的幫助……顯示幫助資訊…… >>>help('str')# 檢視 str 資料型別的幫助……顯示幫助資訊…… >>>a = [1,2,3]>>>help(a)# 檢視列表 list 幫助資訊……顯示幫助資訊…… >>>help(a.append)# 顯示list的append方法的幫助……顯示幫助資訊……