怎么使用Python的help語法

這篇文章主要講解了“怎么使用Python的help語法”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么使用Python的help語法”吧!

創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比永寧網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式永寧網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋永寧地區(qū)。費用合理售后完善,10多年實體公司更值得信賴。

一、注釋

確保對模塊, 函數(shù), 方法和行內(nèi)注釋使用正確的風(fēng)格

  1. 單行注釋以 # 開頭

    # 這是一個注釋
    print("Hello, World!")
  2. 單引號(''')

    #!/usr/bin/python3
    '''
    這是多行注釋,用三個單引號
    這是多行注釋,用三個單引號
    這是多行注釋,用三個單引號
    '''
    print("Hello, World!")
  3. 雙引號(""")

    #!/usr/bin/python3
    """
    這是多行注釋,用三個單引號
    這是多行注釋,用三個單引號
    這是多行注釋,用三個單引號
    """
    print("Hello, World!")

二、DIR

  • 語法:dir([object])

  • 說明:

    • 當(dāng)不傳參數(shù)時,返回當(dāng)前作用域內(nèi)的變量、方法和定義的類型列表。

      >>> dir()
      ['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
      >>> a = 10 #定義變量a
      >>> dir() #多了一個a
      ['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'a']
    • 當(dāng)參數(shù)對象是模塊時,返回模塊的屬性、方法列表。

      >>> import math
      >>> math
      <module 'math' (built-in)>
      >>> dir(math)
      ['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
    • 當(dāng)參數(shù)對象是類時,返回類及其子類的屬性、方法列表。

      >>> class A:
          name = 'class'
      >>> a = A()
      >>> dir(a) #name是類A的屬性,其他則是默認(rèn)繼承的object的屬性、方法
      ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name']
    • 當(dāng)對象定義了__dir__方法,則返回__dir__方法的結(jié)果

      >>> class B:
          def __dir__(self):
              return ['name','age']
      >>> b = B()
      >>> dir(b) #調(diào)用 __dir__方法
      ['age', 'name']

三、__doc__

將文檔寫在程序里,是LISP中的一個特色,Python也借鑒過。每個函數(shù)都是一個對象,每個函數(shù)對象都是有一個__doc__的屬性,函數(shù)語句中,如果第一個表達(dá)式是一個string,這個函數(shù)的__doc__就是這個string,否則__doc__是None。

>>> def testfun():
"""
this function do nothing , just demostrate the use of the doc string .
"""
pass
>>> testfun.__doc__
'\nthis function do nothing , just demostrate the use of the doc string .\n'
>>> #pass 語句是空語句,什么也不干,就像C語言中的{} , 通過顯示__doc__,我們可以查看一些內(nèi)部函數(shù)的幫助信息
>>> " ".join.__doc__
'S.join(iterable) -> str\n\nReturn a string which is the concatenation of the strings in the\niterable. The separator between elements is S.'
>>>

四、help

  • 語法:help([object])

  • 說明:

    • 在解釋器交互界面,不傳參數(shù)調(diào)用函數(shù)時,將激活內(nèi)置的幫助系統(tǒng),并進(jìn)入幫助系統(tǒng)。在幫助系統(tǒng)內(nèi)部輸入模塊、類、函數(shù)等名稱時,將顯示其使用說明,輸入quit退出內(nèi)置幫助系統(tǒng),并返回交互界面。

      >>> help() #不帶參數(shù)
       
      Welcome to Python 3.5's help utility!
      If this is your first time using Python, you should definitely check out
      the tutorial on the Internet at 
       
      Enter the name of any module, keyword, or topic to get help on writing
      Python programs and using Python modules.  To quit this help utility and
      return to the interpreter, just type "quit".
       
      To get a list of available modules, keywords, symbols, or topics, type
      "modules", "keywords", "symbols", or "topics".  Each module also comes
      with a one-line summary of what it does; to list the modules whose name
      or summary contain a given string such as "spam", type "modules spam".
       
      #進(jìn)入內(nèi)置幫助系統(tǒng)  >>> 變成了 help>
      help> str #str的幫助信息
      Help on class str in module builtins:
       
      class str(object)
      |  str(object='') -> str
      |  str(bytes_or_buffer[, encoding[, errors]]) -> str
      |
      |  Create a new string object from the given object. If encoding or
      |  errors is specified, then the object must expose a data buffer
      |  that will be decoded using the given encoding and error handler.
      |  Otherwise, returns the result of object.__str__() (if defined)
      |  or repr(object).
      |  encoding defaults to sys.getdefaultencoding().
      |  errors defaults to 'strict'.
      |
      |  Methods defined here:
      |
      |  __add__(self, value, /)
      |      Return self+value.
      ................................
       
      help> 1 #不存在的模塊名、類名、函數(shù)名
      No Python documentation found for '1'.
      Use help() to get the interactive help utility.
      Use help(str) for help on the str class.
       
      help> quit #退出內(nèi)置幫助系統(tǒng)
       
      You are now leaving help and returning to the Python interpreter.
      If you want to ask for help on a particular object directly from the
      interpreter, you can type "help(object)".  Executing "help('string')"
      has the same effect as typing a particular string at the help> prompt.
       
      # 已退出內(nèi)置幫助系統(tǒng),返回交互界面 help> 變成 >>>
    • 在解釋器交互界面,傳入?yún)?shù)調(diào)用函數(shù)時,將查找參數(shù)是否是模塊名、類名、函數(shù)名,如果是將顯示其使用說明。

      >>> help(str)
      Help on class str in module builtins:
       
      class str(object)
      |  str(object='') -> str
      |  str(bytes_or_buffer[, encoding[, errors]]) -> str
      |
      |  Create a new string object from the given object. If encoding or
      |  errors is specified, then the object must expose a data buffer
      |  that will be decoded using the given encoding and error handler.
      |  Otherwise, returns the result of object.__str__() (if defined)
      |  or repr(object).
      |  encoding defaults to sys.getdefaultencoding().
      |  errors defaults to 'strict'.
      |
      |  Methods defined here:
      |
      |  __add__(self, value, /)
      |      Return self+value.
      |
        ***************************

感謝各位的閱讀,以上就是“怎么使用Python的help語法”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對怎么使用Python的help語法這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

分享名稱:怎么使用Python的help語法
當(dāng)前網(wǎng)址:http://muchs.cn/article30/pphiso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、微信公眾號網(wǎng)站制作、網(wǎng)站設(shè)計、網(wǎng)站收錄企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)