[Python3] 037 函数式编程 装饰器
函数式编程 之
装饰器 Decrator
1. 引子
>>> def func():
... print("abc")
...
>>> func()
abc
>>> f = func
>>> f()
abc
>>> id(func) - id(f)
0
>>> f.__name__
'func'
>>>
- 现在由新的需求
- 对 func 进行扩展:每次打印 abc 之前打印当前系统时间
- 但实现这个功能不能改动现有代码
- => 使用装饰器
2. 简介
- 在不改动函数代码的基础上无限制扩展函数功能的一种机制
- 本质上讲,装饰器是一个返回函数的高阶函数
- 装饰器的使用
- 使用 @ 语法
- 即,在每次要扩展的函数定义前使用 @ + 函数名
3. 使用
- 必要的导入
>>> import time
- 先写好函数备用
>>> def print_date(f):
... def wrapper(*args, **kwargs):
... t = time.strftime("%Y-%m-%d", time.localtime())
... print(f"Date: {t}")
... return f(*args, **kwargs)
... return wrapper
...
>>>
3.1 例子1
- 对函数进行功能扩展,每次执行函数前,打印当前日期
>>> @print_date
... def func():
... print("abc")
...
>>> func()
Date: 2019-12-20
abc
>>>
- 装饰器的好处
- 一处定义,多处装饰
- 一旦被装饰,就能拥有装饰器的功能
3.2 例子2
- 不使用 @,手动执行装饰器
>>> def manual():
... print("manual operation")
...
>>> func1 = print_date(manual)
>>> func1()
Date: 2019-12-20
manual operation
>>>
3.4 例子3
- 不一定要像
print_date中的def wrapper(*args, **kwargs)那么标准
>>> def cal_time(f):
... def wrapper(x, y):
... start = time.perf_counter_ns()
... f(x, y)
... stop = time.perf_counter_ns()
... print(f"run time: {stop - start}")
... return wrapper
...
>>> @cal_time
... def add_two_nums(x, y):
... print(f"{x} + {y} = {x + y}")
...
>>> add_two_nums(10, 20)
10 + 20 = 30
run time: 1606600
>>>
- 注意:装饰器要写在被装饰的函数的上方
3.5 例子4
- 两个装饰器
>>> def deco1(f):
... print("decorator1")
... def wrapper():
... print("decorator1's wrapper")
... f()
... return wrapper
...
>>> def deco2(f):
... print("decorator2")
... def wrapper():
... print("decorator2's wrapper")
... f()
... return wrapper
...
>>> @deco2
... @deco1
... def dbl_decos():
... print("double decorators")
...
decorator1
decorator2
>>> dbl_decos()
decorator2's wrapper
decorator1's wrapper
double decorators
>>>
3.6 例子5
- 沿用例子4的思路,但是我换种写法,并把这些代码写到一个 .py 的文件中
def deco1(f):
print("先穿衬衫")
def wrapper():
print("再脱衬衫")
f()
return wrapper
def deco2(f):
print("再穿西装")
def wrapper():
print("先脱西装")
f()
return wrapper
@deco2
@deco1
def dbl_decos():
print("over")
if __name__ == "__main__":
dbl_decos()
>>>
先穿衬衫
再穿西装
先脱西装
再脱衬衫
over
- 装饰的过程:先执行 deco1,再执行 deco2
- 调用的过程:先调用 deco2,再调用 deco1
[Python3] 037 函数式编程 装饰器的更多相关文章
- s14 第4天 关于python3.0编码 函数式编程 装饰器 列表生成式 生成器 内置方法
python3 编码默认为unicode,unicode和utf-8都是默认支持中文的. 如果要python3的编码改为utf-8,则或者在一开始就声明全局使用utf-8 #_*_coding:utf ...
- python函数式编程-装饰器
在代码运行期间动态增加功能的方式,称之为“装饰器”(Decorator). 由于函数也是一个对象,而且函数对象可以赋值给变量,所以通过变量也能调用该函数. >>> def now() ...
- Python实用笔记 (15)函数式编程——装饰器
什么函数可以被称为闭包函数呢?主要是满足两点:函数内部定义的函数:引用了外部变量但非全局变量. python装饰器本质上就是一个函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外的功能,装饰 ...
- Python3(十) 函数式编程: 匿名函数、高阶函数、装饰器
一.匿名函数 1.定义:定义函数的时候不需要定义函数名 2.具体例子: #普通函数 def add(x,y): return x + y #匿名函数 lambda x,y: x + y 调用匿名函数: ...
- [Python3] 035 函数式编程 高阶函数
目录 函数式编程 之 高阶函数 1. 引子 2. 系统提供的高阶函数 3. functools 包提供的 reduce 4. 排序 函数式编程 之 高阶函数 把函数作为参数使用的函数,叫高阶函数 1. ...
- [Python3] 034 函数式编程 匿名函数
目录 函数式编程 Functional Programming 1. 简介 2. 函数 3. 匿名函数 3.1 lambda 表达式也称"匿名函数" 3.2 lambda 表达式的 ...
- python3【基础】-装饰器
要理解充分理解python的装饰器,有充分理解下述三个知识点为前提: python作用域规则 函数即对象 闭包 一.python作用域规则: 首先介绍python中的作用域规则.python的作用域规 ...
- Python 元编程 - 装饰器
Python 中提供了一个叫装饰器的特性,用于在不改变原始对象的情况下,增加新功能或行为. 这也属于 Python "元编程" 的一部分,在编译时一个对象去试图修改另一个对象的信息 ...
- 循序渐进Python3(四) -- 装饰器、迭代器和生成器
初识装饰器(decorator ) Python的 decorator 本质上就是一个高阶函数,它接收一个函数作为参数,然后,返回一个新函数. 使用 decorator 用Python提供的 @ 语法 ...
随机推荐
- Maven - Maven Project与Maven Module区别和联系
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/J080624/article/detai ...
- maven工程的简单构建
1.按maven约定的目录结构创建文件夹 约定目录结构:不按约定的目录来建maven无法正常工作: Hello |---src |---|---main ...
- Ubuntu:19.04程序启动缓慢
造冰箱的大熊猫@cnblogs 2019/5/8 Ubuntu 19.04,程序(Firefox.Terminal.Nautilus)启动非常缓慢.点击桌面工具栏上的Nautilus图标后,隔了很久才 ...
- CF #366 DIV2 C. Thor 模拟 queue/stack降低复杂度
C. Thor time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- HFUUOJ1023 闷声发大财 概率dp
题意 xyq有\(n\)个骰子,第\(i\)个骰子有\(a_i\)面,每次xyq都会把\(n\)个骰子搞一遍,其中的最小值作为结果,问最终结果的期望\(\mod (10^9+7 )\). 分析 lfx ...
- Fragment中 监听Android 返回按钮事件
@Override public void onResume() { super.onResume(); getView().setFocusableInTouchMode(true); getVie ...
- gitk、Git GUI 图形化工具中文显示乱码的解决方案
在Windows下使用gitk.Git-Gui时,可能会出现代码中的中文乱码的情况.解决方法:在软件的安装目录下,在Git\mingw64\etc\gitconfig文件末尾添加: [gui]enco ...
- ansible模块文件操作
Ansible常用模块文件操作 [root@tiandong etc]# ansible-doc -l 列出ansible所支持的模块 [root@tiandong ~]# ansible-doc ...
- Android学习_数据持久化
数据持久化:将内存中的瞬时数据存储到设备中 1. 文件存储 存储一些简单的文本数据或二进制数据. 核心:Context类提供的openFileOutput()和openFileInput()方法,然后 ...
- js实现图片上传到服务器和回显
关于js实现图片的上传和回显,曾经用户的代码粘在这里: 样式:这样写样式的道理是给<input>标签的父级设置一个背景图,就是‘+’那个背景图,然后把<input>的宽高设置得 ...