装饰器--decorator3
装饰器添加返回值
import time
def timer(func):
def wrapper(*args,**kwargs): #wrapper包装的意思
start_time = time.time()
func(*args,**kwargs)
stop_time = time.time()
print("The run time of the function is {}".format(stop_time-start_time))
return wrapper @timer
def cacl(number):
start_num = 1
total = 0
while start_num < number:
if start_num % 2 == 1:
total += start_num
else:
start_num += 1
continue
start_num += 1
return total print(cacl(1000000)) #输出
The run time of the function is 0.172149658203125
None
我们定义一个函数用来计算1000000内奇数之和,并返回其计算结果。同样我们使用一个装饰器来计算该程序的运行时间。可以看到我们最后的运行结果中,返回值是 None
我们已经知道如下:
- @timer ---> cacl = wrapper(cacl)
- cacl()函数当中有返回值,但是 wrapper()函数中没有返回值,所以最后输出的结果为 None
增加返回结果
import time
def timer(func):
def wrapper(*args,**kwargs): #wrapper包装的意思
start_time = time.time()
res = func(*args,**kwargs)
stop_time = time.time()
print("The run time of the function is {}".format(stop_time-start_time))
return res #添加一个返回值
return wrapper @timer
def cacl(number):
start_num = 1
total = 0
while start_num < number:
if start_num % 2 == 1:
total += start_num
else:
start_num += 1
continue
start_num += 1
return total #输出
The run time of the function is 0.169450044631958
250000000000
装饰器--decorator3的更多相关文章
- JAVA装饰器模式
Java程序员们应该对java.io对不会陌生,因为java.io包采用了装饰器模式. 一.定义: Decorator装饰器,顾名思义,就是动态地给一个对象添加一些额外的职责,就好比为房子进行装修一样 ...
- python装饰器小计
1.装饰器:本质是函数,是用来给其他函数添加附加扩展功能的函数,其返回值是一个函数(函数指针) 2.装饰器作用:不改变函数源代码和函数调用方式的前提下添加函数的附加功能. 3.装饰器储备知识点: A. ...
- python装饰器1:函数装饰器详解
装饰器1:函数装饰器 装饰器2:类装饰器 装饰器3:进阶 先混个眼熟 谁可以作为装饰器(可以将谁编写成装饰器): 函数 方法 实现了__call__的可调用类 装饰器可以去装饰谁(谁可以被装饰): 函 ...
- python装饰器执行顺序
. python 装饰器 1) 2层装饰器 def decorator(func): # TODO def wrapper(*args, **kwargs): # TODO func(*args, * ...
- Python进阶: Decorator 装饰器你太美
函数 -> 装饰器 函数的4个核心概念 1.函数可以赋与变量 def func(message): print('Got a message: {}'.format(message)) send ...
- Python核心技术与实战——十四|Python中装饰器的使用
我在以前的帖子里讲了装饰器的用法,这里我们来具体讲一讲Python中的装饰器,这里,我们从前面讲的函数,闭包为切入点,引出装饰器的概念.表达和基本使用方法.其次,我们结合一些实际工程中的例子,以便能再 ...
- Python 简明教程 --- 22,Python 闭包与装饰器
微信公众号:码农充电站pro 个人主页:https://codeshellme.github.io 当你选择了一种语言,意味着你还选择了一组技术.一个社区. 目录 本节我们来介绍闭包与装饰器. 闭包与 ...
- Python高手之路【四】python函数装饰器
def outer(func): def inner(): print('hello') print('hello') print('hello') r = func() print('end') p ...
- python装饰器
今天看了装饰器的一些内容,感觉@修饰符还是挺抽象的. 装饰器就是在不用改变函数实现的情况下,附加的实现一些功能,比如打印日志信息等.需要主意的是装饰器本质是一个高阶函数,她可以返回一个函数. 装饰器需 ...
随机推荐
- Windows和Linux系统如何退出python命令行
python命令行是新手学习python过程中必须要学的一个工具,下面我们来看一下怎么退出python命令行. 第一种方式: 使用python提供的exit()函数,linux平台和windows平台 ...
- Spring Task中的定时任务无法注入service的解决办法
1.问题 因一个项目(使用的是Spring+SpringMVC+hibernate框架)需要在spring task定时任务中调用数据库操作,在使用 @Autowired注入service时后台报错, ...
- 一个简单的rest_framework demo
models.py from django.db import models class UserInfo(models.Model): username = models.CharField(max ...
- python3【基础】-字符串 常用的方法
字符串一个最重要的特性就是不可修改. name.capitalize() 首字母大写 name.casefold() 大写全部变小写 name.center(50,"-") 输出 ...
- POJ 3784 Running Median(动态维护中位数)
Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...
- 第13章 学习shell script
由于博客园中dollar符号有别的意义,所以文中的dollar符号使用¥表示 第一个script [root@localhost script]# cat -n sh01.sh #!/bin/bash ...
- OSG学习:LOD、数据分页、动态调度
LOD(level of detail):是指根据物体模型的结点在显示环境中所处的位置和重要度,决定物体渲染的资源分配,降低非重要物体的面数和细节度,从而获得高效率的渲染运算.在OSG的场景结点组织结 ...
- Markdown github 风格语法
某些效果cnblog无法支持,见 https://github.com/tanghammer/note/blob/master/Markdown%20github%E9%A3%8E%E6%A0%BC% ...
- 运维堡垒机----Gateone
简介: 运维堡垒机的理念起源于跳板机.2000年左右,高端行业用户为了对运维人员的远程登录进行集中管理,会在机房里部署跳板机.跳板机就是一台服务器,维护人员在维护过程中,首先要统一登录到这台服务器上, ...
- HDU4759_Poker Shuffle
这是一个很好的题目,来自2013长春网赛. 题目的意思是给你2^N张扑克牌,每次洗牌前分别把从下开始数为奇数和偶数的牌分别拿出来放在一堆,两堆可以任意一个放在上面. 现在问你是否存在一种情况使得经过若 ...