欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

怎么使用Python的help語法-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)是專業(yè)的鞏留網站建設公司,鞏留接單;提供成都網站建設、成都網站制作,網頁設計,網站設計,建網站,PHP網站建設等專業(yè)做網站服務;采用PHP框架,可快速的進行鞏留網站開發(fā)網頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網站,專業(yè)的做網站團隊,希望更多企業(yè)前來合作!

一、注釋

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

  1. 單行注釋以 # 開頭

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

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

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

二、DIR

  • 語法:dir([object])

  • 說明:

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

      >>> dir()
      ['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
      >>> a = 10 #定義變量a
      >>> dir() #多了一個a
      ['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'a']
    • 當參數(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']
    • 當參數(shù)對象是類時,返回類及其子類的屬性、方法列表。

      >>> class A:
          name = 'class'
      >>> a = A()
      >>> dir(a) #name是類A的屬性,其他則是默認繼承的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']
    • 當對象定義了__dir__方法,則返回__dir__方法的結果

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

三、__doc__

將文檔寫在程序里,是LISP中的一個特色,Python也借鑒過。每個函數(shù)都是一個對象,每個函數(shù)對象都是有一個__doc__的屬性,函數(shù)語句中,如果第一個表達式是一個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__,我們可以查看一些內部函數(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ù)調用函數(shù)時,將激活內置的幫助系統(tǒng),并進入幫助系統(tǒng)。在幫助系統(tǒng)內部輸入模塊、類、函數(shù)等名稱時,將顯示其使用說明,輸入quit退出內置幫助系統(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".
       
      #進入內置幫助系統(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 #退出內置幫助系統(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.
       
      # 已退出內置幫助系統(tǒng),返回交互界面 help> 變成 >>>
    • 在解釋器交互界面,傳入參數(shù)調用函數(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語法”的內容了,經過本文的學習后,相信大家對怎么使用Python的help語法這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關知識點的文章,歡迎關注!

網站名稱:怎么使用Python的help語法-創(chuàng)新互聯(lián)
文章來源:http://chinadenli.net/article0/cdpoio.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、用戶體驗、App開發(fā)建站公司、全網營銷推廣、外貿網站建設

廣告

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

搜索引擎優(yōu)化