python开发手册和函数文档的使用

1.python中通过是用help函数我们可以快速的查看函数的使用文档,例如:

help(print)

Help on built-in function print in module builtins:

print(*args, sep=' ', end='\n', file=None, flush=False)##函数原型
    Prints the values to a stream, or to sys.stdout by default.
    ##函数的功能介绍

    ##各个参数的类型及作用
    sep
      string inserted between values, default a space.
    end
      string appended after the last value, default a newline.
    file
      a file-like object (stream); defaults to the current sys.stdout.
    flush
      whether to forcibly flush the stream.

通过使用字符串来创建函数文档

def exchange(dollar, rate=7.32):
    """
    功能:汇率转换,美元->人民币
    - dollar 美元数量
    - rate 汇率,默认值是7.32
    返回值:
    - 人民币的数量
    """
    return dollar * rate

print(exchange(20))
##146.4

help(exchange)
## exchange(dollar, rate=7.32)
##     功能:汇率转换,美元->人民币
##     - dollar 美元数量
##     - rate 汇率,默认值是7.32
##     返回值:
##     - 人民币的数量

 2.类型注释:

    函数中冒号后是对函数参数的注释,箭头后是对返回值的注释,但是要注意的是即使不按注释输入,程序依然不会报错。在注释后使用赋值号依然可以使用默认参数.
def times(s:str, n:int = 5) -> str:
    return s * n

print(times("Cloud"))
## CloudCloudCloudCloudCloud

print(times("Cloud", 3))
## CloudCloudCloud

同理我们也可以对输入序列以及其参数参数类型进行注释:

def times(s:list[int], n:int = 5) -> list:
    return s*n

print(times([1, 2, 3]))
## [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]



def times1(s:dict[str, int], n:int = 5) -> list:
    return list(s.keys())*n

print(times1({'a':1, 'b':2, 'c':3}))
## ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']

3.内省

def times(s:dict[str, int], n:int = 5) -> list:
    return list(s.keys())*n

print(times.__name__)
## times
# 查看函数名称

print(times.__annotations__)
## {'s': dict[str, int], 'n': <class 'int'>, 'return': <class 'list'>}
# 查看函数的类型注释

print(print.__doc__)
##Prints the values to a stream, or to sys.stdout by default.
##
##  sep
##    string inserted between values, default a space.
##  end
##    string appended after the last value, default a newline.
##  file
##    a file-like object (stream); defaults to the current sys.stdout.
##  flush
##    whether to forcibly flush the stream.

#查看函数文档

1. 开胃菜 2. 使用 Python 解释器 2.1. 调用 Python 解释器 2.1.1. 参数传递 2.1.2. 交互模式 2.2. 解释器及其环境 2.2.1. 错误处理 2.2.2. 执行 Python 脚本 2.2.3. 源程序编码 2.2.4. 交互执行文件 2.2.5. 本地化模块 3. Python 简介 3.1. 将 Python 当做计算器 3.1.1. 数字 3.1.2. 字符串 3.1.3. 关于 Unicode 3.1.4. 列表 3.2. 编程的第一步 4. 深入 Python 流程控制 4.1. if 语句 4.2. for 语句 4.3. The range() 函数 4.4. break continue 语句, 以及循环中的 else 子句 4.5. pass 语句 4.6. 定义函数 4.7. 深入 Python 函数定义 4.7.1. 默认参数值 4.7.2. 关键字参数 4.7.3. 可变参数列表 4.7.4. 参数列表的分拆 4.7.5. Lambda 形式 4.7.6. 文档字符串 4.8. 插曲:编码风格 5. 数据结构 5.1. 关于列表更多的内容 5.1.1. 把链表当作堆栈使用 5.1.2. 把链表当作队列使用 5.1.3. 列表推导式 5.1.4. 嵌套的列表推导式 5.2. del 语句 5.3. 元组序列 5.4. 集合 5.5. 字典 5.6. 循环技巧 5.7. 深入条件控制 5.8. 比较序列其它类型 6. 模块 6.1. 深入模块 6.1.1. 作为脚本来执行模块 6.1.2. 模块的搜索路径 6.1.3. “编译的” Python 文件 6.2. 标准模块 6.3. dir() 函数 6.4. 包 6.4.1. 从 * 导入包 6.4.2. 包内引用 6.4.3. 多重目录中的包 7. 输入输出 7.1. 格式化输出 7.1.1. 旧式的字符串格式化 7.2. 文件读写 7.2.1. 文件对象方法 7.2.2. pickle 模块 8. 错误异常 8.1. 语法错误 8.2. 异常 8.3. 异常处理 8.4. 抛出异常 8.5. 用户自定义异常 8.6. 定义清理行为 8.7. 预定义清理行为 9. 类 9.1. 术语相关 9.2. Python 作用域命名空间 9.2.1. 作用域命名空间示例 9.3. 初识类 9.3.1. 类定义语法 9.3.2. 类对象 9.3.3. 实例对象 9.3.4. 方法对象 9.4. 一些说明 9.5. 继承 9.5.1. 多继承 9.6. 私有变量 9.7. 补充 9.8. 异常也是类 9.9. 迭代器 9.10. 生成器 9.11. 生成器表达式 10. Python 标准库概览 10.1. 操作系统接口 10.2. 文件通配符 10.3. 命令行参数 10.4. 错误输出重定向程序终止 10.5. 字符串正则匹配 10.6. 数学 10.7. 互联网访问 10.8. 日期时间 10.9. 数据压缩 10.10. 性能度量 10.11. 质量控制 10.12. “瑞士军刀” 11. 标准库浏览 – Part II 11.1. 输出格式 11.2. 模板 11.3. 使用二进制数据记录布局 11.4. 多线程 11.5. 日志 11.6. 弱引用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值