python函数注释与函数声明

python注释

def  f(text:str, max_len:'int>0'=80) ->str:
  """在这里备注函数说明,help时会显示"""
  return true

"""
函数声明中,text:str
text 是参数 :冒号后面  str是参数的注释。
如果参数有默认值,还要给注释,如下写。
max_len:'int>0'=80

->str 是函数返回值的注释。

这些注释信息都是函数的元信息,保存在f.__annotations__字典中、

需要注意,python对注释信息和f.__annotations__的一致性,不做检查
不做检查,不做强制,不做验证!什么都不做。
"""
"""函数注释示例:"""
def f(ham: 42, eggs: int = 'spam') -> "Nothing to see here":
    print("函数注释", f.__annotations__)
    print("参数值打印", ham, eggs)
    print(type(ham),type(eggs))

f("www")
返回信息:
函数注释 {'ham': 42, 'eggs': <class 'int'>, 'return': 'Nothing to see here'}
参数值打印 www spam
<class 'str'> <class 'str'>
解释说明:
注释的一般规则是参数名后跟一个冒号(:),然后再跟一个expression,这个expression可以是任何形式。

返回值的形式是 -> int,annotation可被保存为函数的attributes。


Google风格
"""
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风格
"""
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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容