python函數(shù)注釋與函數(shù)聲明

python注釋

def  f(text:str, max_len:'int>0'=80) ->str:
  """在這里備注函數(shù)說明,help時會顯示"""
  return true

"""
函數(shù)聲明中,text:str
text 是參數(shù) :冒號后面  str是參數(shù)的注釋。
如果參數(shù)有默認值,還要給注釋,如下寫。
max_len:'int>0'=80

->str 是函數(shù)返回值的注釋。

這些注釋信息都是函數(shù)的元信息,保存在f.__annotations__字典中、

需要注意,python對注釋信息和f.__annotations__的一致性,不做檢查
不做檢查,不做強制,不做驗證!什么都不做。
"""
"""函數(shù)注釋示例:"""
def f(ham: 42, eggs: int = 'spam') -> "Nothing to see here":
    print("函數(shù)注釋", f.__annotations__)
    print("參數(shù)值打印", ham, eggs)
    print(type(ham),type(eggs))

f("www")
返回信息:
函數(shù)注釋 {'ham': 42, 'eggs': <class 'int'>, 'return': 'Nothing to see here'}
參數(shù)值打印 www spam
<class 'str'> <class 'str'>
解釋說明:
注釋的一般規(guī)則是參數(shù)名后跟一個冒號(:),然后再跟一個expression,這個expression可以是任何形式。

返回值的形式是 -> int,annotation可被保存為函數(shù)的attributes。


Google風(fēng)格
"""
This is a groups style docs.

Parameters:
  param1 - this is the first param
  param2 - this is a second param

Returns:
    This is a description of what is returned

Raises:
    KeyError - raises an exception
"""

Rest風(fēng)格
"""
This is a reST style.

:param param1: this is a first param
:param param2: this is a second param
:returns: this is a description of what is returned
:raises keyError: raises an exception
"""

引用
[1]https://blog.csdn.net/sunt2018/article/details/83022493

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容