Skip to content

Latest commit

 

History

History
66 lines (34 loc) · 1.04 KB

5.5函数提示文档的定义.md

File metadata and controls

66 lines (34 loc) · 1.04 KB

函数提示文档的定义

1. 背景

2. 在交互模式中定义

进入到交互模式:

>>> def test(a,b):
...     "用来完成对2个数求和"  # 函数第一行写一个字符串作为函数文档
...     print("%d"%(a+b))
... 
>>> 
>>> test(11,22)  # 函数可以正常调用
33
>>>
>>> help(test)  # 使用 help 查看test函数的文档说明
Help on function test in module __main__:

test(a, b)
    用来完成对2个数求和

使用效果如下:

3. 更多的中文提示

def get_info(name: str, age: int):
    """
    接收用户的名字和年龄,拼接一个字符串并返回

    :param name: 接收一个名字
    :param age: 接收用户的年龄,必须是 0-200 间的一个整数
    :return: 返回拼接好的字符串
    """
    return "我的名字叫 %s,今年是 %d 岁" % (name, age)


get_info("吴彦祖", 19)
get_info(520, 19)  # 注意,形参上标注的类型只是提高代码的可读性,并不会限制实参的类型
help(get_info)

使用效果如下: