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