风火编程--《python核心编程》读书笔记(六)

版权声明:风火编程, 欢迎指正. https://blog.csdn.net/weixin_42620314/article/details/83904362

python核心编程–第二版

第十一章

11.3.6装饰器
def 装饰器函数(func):
def wrapper(*args, **kwargs):
before_func()
func()
after_func()
return wrapper
使用默认参数可以提高程序的鲁棒性

11.7(函数式)编程
返回list的累乘

reduce(lambda x, y: x*y, list)

偏函数

add1 = partial(add, 1)

闭包
在外层函数里定义内层函数, 并在定义内层函数时使用外层函数的变量.
该变量叫做自由变量.
自由变量可以在内层重新赋值,相当于在内层域中重新定义了一个同名的变量
修改自由变量需要用nonlocal声明

def out():
    x=y=1
    def inn():
        x = 2
        nonlocal x
       # x = 2
        x += 2
        print("inn:",x,y)
    print("out:",x,y)
    return inn
inn = out()
inn()

11.10.2生成器
next() # 返回下一个元素
send(element) # 加入新元素并返回

第十二章

12.4模块的导入
顺序
标准库模块
三方库模块
自定义模块
推荐使用from—import (—, )

猜你喜欢

转载自blog.csdn.net/weixin_42620314/article/details/83904362
今日推荐