怎么使用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ū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。

一、注釋

確保對模塊, 函數(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語法這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

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

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

廣告

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

小程序開發(fā)
主站蜘蛛池模板: 无码精品国产va在线观看dvd| 免费无码一区二区三区蜜桃大| 亚洲AV永久无码天堂影院| 亚洲aⅴ无码专区在线观看春色| 无码av人妻一区二区三区四区 | 免费无码不卡视频在线观看| 无码一区二区三区在线| 亚洲AV无码一区东京热| 无码一区二区三区亚洲人妻| 久久久久无码国产精品一区| 日韩美无码五月天| 精品无码综合一区二区三区| 亚洲va中文字幕无码久久| 亚洲精品无码你懂的网站| 亚洲爆乳无码专区www| AV无码免费永久在线观看| 国产V亚洲V天堂无码久久久| 一本久道中文无码字幕av| 伊人久久大香线蕉无码| 亚洲Av无码精品色午夜| 成人无码a级毛片免费| 曰韩无码二三区中文字幕| 亚洲AV日韩AV无码污污网站| 无码h黄动漫在线播放网站| 18禁超污无遮挡无码免费网站| yy111111少妇无码影院| 国产成人无码a区在线观看视频免费 | 无码专区国产无套粉嫩白浆内射| 亚洲成a∧人片在线观看无码| 精品无码人妻一区二区三区| 亚洲AV永久无码精品一百度影院 | 亚洲精品无码久久久久久久| 亚洲爆乳无码专区| 高h纯肉无码视频在线观看| 宅男在线国产精品无码| 免费看无码特级毛片| 久久久久久国产精品无码下载| 日本精品无码一区二区三区久久久| 亚洲AV永久无码精品网站在线观看| 久久亚洲精品成人av无码网站| av无码免费一区二区三区|