电商网站怎么做与众不同,大连seo建站,aspcms网站源码,网站建设平ppt函数 (part 5)
本节主要讲函数文档、类型注释、内省、高阶函数
函数文档、类型注释、内省 (P52)
函数文档
函数是一种代码封装的方法#xff0c;对于一个程序来说#xff0c;函数就是一个结构组件。在函数的外部是不需要关心函数内部的执行细节的#xff0c;更需要关注的…函数 (part 5)
本节主要讲函数文档、类型注释、内省、高阶函数
函数文档、类型注释、内省 (P52)
函数文档
函数是一种代码封装的方法对于一个程序来说函数就是一个结构组件。在函数的外部是不需要关心函数内部的执行细节的更需要关注的是函数的接口以及执行后的结果。要学会去阅读开发手册和函数文档以快速的融入一个项目 在 python 中我们可以使用 help() 函数快速的查看到一个函数的使用文档
help(print)Help on built-in function print in module builtins:print(...)print(value, ..., sep , end\n, filesys.stdout, flushFalse)Prints the values to a stream, or to sys.stdout by default.Optional keyword arguments:file: a file-like object (stream); defaults to the current sys.stdout.sep: string inserted between values, default a space.end: string appended after the last value, default a newline.flush: whether to forcibly flush the stream.# 详解
# Help on built-in function print in module builtins:# print(...)
# print(value, ..., sep , end\n, filesys.stdout, flushFalse) # 展示了 print 函数的原型# Prints the values to a stream, or to sys.stdout by default. # 函数的功能介绍
# Optional keyword arguments: # 各个参数的类型及作用
# file: a file-like object (stream); defaults to the current sys.stdout.
# sep: string inserted between values, default a space.
# end: string appended after the last value, default a newline.
# flush: whether to forcibly flush the stream.如何创建函数文档使用
def exchange(dollar, rate6.32):功能汇率转换美元-人民币参数·dollar 美元数量·rate 汇率默认值是 6.32 (2022-03-08)返回值·人民币数量return dollar * rate
exchange(20)126.4help(exchange)Help on function exchange in module __main__:exchange(dollar, rate6.32)功能汇率转换美元-人民币参数·dollar 美元数量·rate 汇率默认值是 6.32 (2022-03-08)返回值·人民币数量注意函数文档一定是要在函数的最顶部的。
类型注释
示例 比如以下函数作者希望调用者传入到a参数中的类型是字符串类型b参数的类型是整数类型这个函数的返回值将是一个字符串类型。
def func(a:str,b:int) - str:return a*b
func(an,b3)nnn但是这并不是强制的。Python 运行时不强制执行函数和变量类型注解但这些注解可用于类型检查器、IDE、静态检查器等第三方工具。即并不是设置类型后像 java 那样的强类型定义语言了。如
func(3,5)15如果我们要设置默认参数
def func(a:stre,b:int5) - str:return a*b
func()eeeee如果说我们期待的参数类型是列表甚至一个整数列表所有值都是整数的列表
def func(a:list,b:int3) - list:return a*b
func([1,2,3])[1, 2, 3, 1, 2, 3, 1, 2, 3]def func(a:list[int],b:int3) - list:return a*b
func([1,2,3])[1, 2, 3, 1, 2, 3, 1, 2, 3]映射类型也可实现比如我们期待的字典的键是字符串而值是整数
def func(a:dict[str,int],b:int3) - list:key list(a.keys())*bv list(a.values())*breskeyvreturn res
func({a:1,b:2,c:3})[a, b, c, a, b, c, a, b, c, 1, 2, 3, 1, 2, 3, 1, 2, 3]如果我们想让python帮我们做一下检测可以使用 Mypy 模块
内省
在程序运行的时候能够进行自我检测的一种机制称之为内省或自省 Python 是通过一些特殊的属性来实现内省的 求知一个函数的名字
exchange.__name__exchange求知一个函数的类型注释
func.__annotations__{a: dict[str, int], b: int, return: list}查看函数文档
exchange.__doc__\n 功能汇率转换美元-人民币\n 参数\n ·dollar 美元数量\n ·rate 汇率默认值是 6.32 (2022-03-08)\n 返回值\n ·人民币数量\n print(exchange.__doc__)功能汇率转换美元-人民币参数·dollar 美元数量·rate 汇率默认值是 6.32 (2022-03-08)返回值·人民币数量高阶函数 (higher-order function) (P53)
当一个函数接收另一个函数作为参数的时候那么这个时候这种函数就叫高阶函数 其实在前面的装饰器那一节我们就碰到过高阶函数
# 前面装饰器的例子
import time
def time_master(func):def call_func():print(start)start time.time()func()stop time.time()print(stop)print(fIn total we used {(stop-start):.2f} second)return call_funcdef myfunc():time.sleep(2)print(Hi)myfunc time_master(myfunc)
myfunc()start
Hi
stop
In total we used 2.00 second在这里 time_master() 把 myfunc() 作为参数使用time_master()就是一个高阶函数。 Pyhton 中常见的高阶函数有 map, filter, 除此外min, max, salty也可以算是高阶因为他们有一个 key 参数接收的就是一个函数对象。
functools 模块
Python 中收集实用的一些高阶函数以及装饰器的模块
reduce() 函数
reduce() 函数有两个参数第一个参数是一个函数第二个参数是一个可迭代对象。
def add_f(a,b):return ab
import functools
functools.reduce(add_f, [1,2,3,4,5])15这里 reduce 的作用就是将可迭代对象中的元素依次传递到第一个参数指定的函数中最终返回累积的结果。 其实就相当于
add_f(add_f(add_f(add_f(1,2),3),4),5)15reduce() 函数的第一个参数是一个函数自然也可以是 lambda 表达式。比如计算10的阶乘
functools.reduce(lambda x,y:x*y, range(1,11))3628800偏函数
偏函数是指对指定的函数进行二次包装通常是对现有的函数部分参数预先绑定从而得到一个新的函数则该函数称之为偏函数。 偏函数通过 functools 里的 partial 来实现 偏函数的作用就是将一个函数的多个参数给拆分多次进行传递。如
square functools.partial(pow,2)
print(square(2))
print(square(3))
print(square(5))4
8
32cube functools.partial(pow,3)
print(cube(2))
print(cube(3))
print(cube(5))9
27
243和我们之前讲到闭包一样实现这个功能其实这个偏函数的实现原理就是闭包。
wraps
我们回到前面装饰器 time_master 和 myfunc 的例子
def time_master(func):def call_func():print(start)start time.time()func()stop time.time()print(stop)print(fIn total we used {(stop-start):.2f} second)return call_functime_master
def myfunc():time.sleep(2)print(Hi)myfunc()start
Hi
stop
In total we used 2.01 second但是这里有一个副作用调用myfunc.__name__ 会得到 call_func 而不是 myfunc
myfunc.__name__call_func装饰器其实是一个语法糖代码其实相当于
def time_master(func):def call_func():print(start)start time.time()func()stop time.time()print(stop)print(fIn total we used {(stop-start):.2f} second)return call_funcdef myfunc():time.sleep(2)print(Hi)myfunc time_master(myfunc)
myfunc()start
Hi
stop
In total we used 2.01 second由于闭包的设计调用 myfunc 函数其实是调用了 time_master 函数然后传入 myfunc 作为其参数。而调用 time_master 函数其实是调用 call_func 函数。所以当使用 name 属性内省的时候就是 call_func 了。 其实实际运用的时候影响不大但是如果想纠正这个问题可以用 wraps 装饰器来装饰装饰器。
def time_master(func):functools.wraps(func)def call_func():print(start)start time.time()func()stop time.time()print(stop)print(fIn total we used {(stop-start):.2f} second)return call_funcdef myfunc():time.sleep(2)print(Hi)myfunc time_master(myfunc)
myfunc()start
Hi
stop
In total we used 2.00 secondmyfunc.__name__myfunc# 或者装饰器的形式
def time_master(func):functools.wraps(func)def call_func():print(start)start time.time()func()stop time.time()print(stop)print(fIn total we used {(stop-start):.2f} second)return call_functime_master
def myfunc():time.sleep(2)print(Hi)myfunc()start
Hi
stop
In total we used 2.01 secondmyfunc.__name__myfunc附言 题目Self-study Python Fish-C Note-15 P52-P53 本文为自学B站上鱼C的python课程随手做的笔记。一些概念和例子我个人为更好的理解做了些查询和补充 因本人水平有限如有任何问题欢迎大家批评指正 原视频链接https://www.bilibili.com/video/BV1c4411e77t?p8