python学习,day4:装饰器的使用示例
---恢复内容开始---
装饰器:本质是函数,(装饰其他函数)就是为其他函数添加附加功能
装饰器有其独特的原则:1、不能修改被装饰的函数的源代码
2、不能修改被装饰的函数的调用方式
例子
import time
def timmer(func): #装饰器
def warpper(*args,**kwargs):
start_time = time.time()
func()
stop_time =time.time()
print('the func run time is %s' %(stop_time - start_time))
return warpper @timmer def test(): #被修饰函数
time.sleep(3)
print('in the test') test()
实现装饰器只是储备:
1、函数即“变量”
2、阶段函数
a.把一个函数当做实参传给另外一个函数(在不修改被装饰函数源代码的情况下为其添加功能)
import time
def bar():
time.sleep(3)
print('in the bar')
def test(func):
time_start = time.time()
func()
time_stop = time.time()
print('running time is %s'%(time_stop-time_start)) test(bar)
b。返回值中包含函数名(不修改函数的调用方式)
def test2(func):
print(func)
return func
#print(test2(bar)) bar = test2(bar)
bar()
3、嵌套函数
x=0
def granda():
x=1
def dad():
x=2
def son():
x=3
print(x)
son() #必须有,要运行才能生效,否则会跳过
dad()
granda()
高洁函数+嵌套函数 =》装饰器
装饰器使用
# coding=utf-8
# Author: RyAn Bi
import time def timer(func): #timer(test1) func = test1
def deco():
start_time = time.time()
func() #run test1
stop_time = time.time()
print('the func running time is %s'%(stop_time-start_time))
return deco
@timer #等同于 test1 = timer(test1) 调用装饰器
def test1():
time.sleep(2)
print('in the test1')
@timer #等同于 test2 = timer(test2) 调用装饰器
def test2():
time.sleep(2)
print('in the test2') # t = timer(test1) #t 获得了deco的地址,地址存的是deco运行的结果
# t() #调出结果 test1()
test2()
python学习,day4:装饰器的使用示例的更多相关文章
- python 学习分享-装饰器篇
本篇内容为偷窃的~哈哈,借用一下,我就是放在自己这里好看. 引用地址:http://www.cnblogs.com/rhcad/archive/2011/12/21/2295507.html 第一步: ...
- python学习笔记--装饰器
1.首先是一个很无聊的函数,实现了两个数的加法运算: def f(x,y): print x+y f(2,3) 输出结果也ok 5 2.可是这时候我们感觉输出结果太单一了点,想让代码的输出多一点看起来 ...
- python学习之装饰器-
python的装饰器 2018-02-26 在了解python的装饰器之前我们得了解python的高阶函数 python的高阶函数我们能返回一个函数名并且能将函数名作为参数传递 def outer() ...
- python学习day14 装饰器(二)&模块
装饰器(二)&模块 #普通装饰器基本格式 def wrapper(func): def inner(): pass return func() return inner def func(): ...
- python学习 day13 装饰器(一)&推导式
装饰器&推导式 传参位置参数在前,关键词参数在后 函数不被调用内部代码不被执行 函数在被调用的时候,每次都会开辟一个新的内存地址,互不干扰 #经典案例 def func(num): def i ...
- Python学习 :装饰器
装饰器(函数) 装饰器作为一个函数,可以为其他函数在不修改原函数代码的前提下添加新的功能 装饰器的返回值是一个函数对象.它经常用于有切面需求的场景,比如:插入日志.性能测试.事务处理.缓存.权限校验等 ...
- python学习之-- 装饰器
高阶函数+嵌套函数 == 装饰器 什么是装饰器: 其实也是一个函数. 功能:为其他的函数添加附加功能 原则:不能修改被装饰的函数的源代码和调用方式 学习装饰器前首先要明白以下3条事项: 1:函数 即 ...
- Python学习笔记--装饰器的实验
装饰器既然可以增加原来函数的功能,那能不能改变传给原函数的参数呢? 我们实验一下,先上代码: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date ...
- 6月4日 python学习总结 装饰器复习
1. 装饰器的原理以及为什么要使用装饰器 在代码运行期间动态增加功能的方式,称之为"装饰器"(Decorator). 在不影响原代码结构的情况下为其添加功能 2. 装饰器的基本 ...
- Python学习之装饰器进阶
函数知识回顾: 函数的参数分为:实参和形参. 实参:调用函数的时候传入的参数: 形参:分为3种(位置参数.默认参数.动态传参) 位置参数:必须传值 def aaa(a,b): print(a,b) a ...
随机推荐
- linux计划任务(一)
一次性计划任务 at /etc/init.d/atd [root@localhost ~]# at : at> /bin/ls /etc |wc -l > /tmp/yimiao_demo ...
- HDU2544 最短路 2017-04-12 18:51 31人阅读 评论(0) 收藏
最短路 Time Limit : 5000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissio ...
- EBS通过SQL查找所有的定时请求
--查找所有定时请求. --也可以登录系统,在系统管理员下查找特定请求,状态设置为Scheduled进行查询SELECT DISTINCT USER_CONCURRENT_PROGRAM_NAME,B ...
- python经典书记必读:Python编程快速上手 让繁琐工作自动化
所属网站分类: 资源下载 > python电子书 作者:熊猫烧香 链接:http://www.pythonheidong.com/blog/article/69/ 来源:python黑洞网,专注 ...
- Http 安全检测
httpsecurityreport.com www.ssllabs.com
- DataTables使用总结
一.使用方法 1.引入JS文件 <script src="js/plugin/datatables/jquery.dataTables.min.js">< ...
- 学习React前端框架,报错 'render' is not defined no-undef
报错 'render' is not defined no-undef 原因没有 写 import { render } from 'react-dom'
- Working Set
类似于Visual Studio中的Solution 如果Eclipse中的project过多,而且不是同一个真实的项目中的,可以按Working Set对project进行组织,只是一个逻辑组织 操 ...
- iOS Keychain 跨应用
Keychain 可以用来持久保存一些信息.通常每个应用都有自己的 Keychain Access.但有时你会需要多个应用共用一些信息.这时需要创建 Keychain Access Group. Ke ...
- qi zi
#include<stdio.h> ]; ][]; int N; typedef struct node{ int x; }node; node dui[]; int se(int a) ...