---恢复内容开始---

装饰器:本质是函数,(装饰其他函数)就是为其他函数添加附加功能

装饰器有其独特的原则: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:装饰器的使用示例的更多相关文章

  1. python 学习分享-装饰器篇

    本篇内容为偷窃的~哈哈,借用一下,我就是放在自己这里好看. 引用地址:http://www.cnblogs.com/rhcad/archive/2011/12/21/2295507.html 第一步: ...

  2. python学习笔记--装饰器

    1.首先是一个很无聊的函数,实现了两个数的加法运算: def f(x,y): print x+y f(2,3) 输出结果也ok 5 2.可是这时候我们感觉输出结果太单一了点,想让代码的输出多一点看起来 ...

  3. python学习之装饰器-

    python的装饰器 2018-02-26 在了解python的装饰器之前我们得了解python的高阶函数 python的高阶函数我们能返回一个函数名并且能将函数名作为参数传递 def outer() ...

  4. python学习day14 装饰器(二)&模块

    装饰器(二)&模块 #普通装饰器基本格式 def wrapper(func): def inner(): pass return func() return inner def func(): ...

  5. python学习 day13 装饰器(一)&推导式

    装饰器&推导式 传参位置参数在前,关键词参数在后 函数不被调用内部代码不被执行 函数在被调用的时候,每次都会开辟一个新的内存地址,互不干扰 #经典案例 def func(num): def i ...

  6. Python学习 :装饰器

    装饰器(函数) 装饰器作为一个函数,可以为其他函数在不修改原函数代码的前提下添加新的功能 装饰器的返回值是一个函数对象.它经常用于有切面需求的场景,比如:插入日志.性能测试.事务处理.缓存.权限校验等 ...

  7. python学习之-- 装饰器

    高阶函数+嵌套函数 == 装饰器 什么是装饰器: 其实也是一个函数. 功能:为其他的函数添加附加功能 原则:不能修改被装饰的函数的源代码和调用方式 学习装饰器前首先要明白以下3条事项: 1:函数 即 ...

  8. Python学习笔记--装饰器的实验

    装饰器既然可以增加原来函数的功能,那能不能改变传给原函数的参数呢? 我们实验一下,先上代码: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date ...

  9. 6月4日 python学习总结 装饰器复习

    1.  装饰器的原理以及为什么要使用装饰器 在代码运行期间动态增加功能的方式,称之为"装饰器"(Decorator). 在不影响原代码结构的情况下为其添加功能 2.  装饰器的基本 ...

  10. Python学习之装饰器进阶

    函数知识回顾: 函数的参数分为:实参和形参. 实参:调用函数的时候传入的参数: 形参:分为3种(位置参数.默认参数.动态传参) 位置参数:必须传值 def aaa(a,b): print(a,b) a ...

随机推荐

  1. 文件读取草稿(excel,csv)

    using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.Data; using ...

  2. 15 Independent Alleles

    Problem Figure 2. The probability of each outcome for the sum of the values on two rolled dice (blac ...

  3. Redis持久化(八)

    Redis特性: (1)多数据库 (2)Redis事物 (3)一个Redis最多可提供16个数据库,下标[0-15] 选择数据库: select 1 (选择1号数据库,默认连接的是0号数据库)移动数据 ...

  4. 网格去噪 Mesh Denoising Guided by Patch Normal Co-filtering via Kernel Low-rank Recovery

    http://staff.ustc.edu.cn/~lgliu/ 网格去噪 https://blog.csdn.net/lafengxiaoyu/article/details/73656060

  5. Linux 基础教程 38-文件下载

    什么是wget     wget用原始帮助里面的英文来讲就是:The non-interactive network downloader,非交互式网络下载器.它支持HTTP.HTTPS.FTP等协议 ...

  6. VS2013的IDE开发使用便捷实用技巧----(补充)

    快捷键操作真的很高效.很酷······ 节省时间,提高开发效率 向人们展示可以惊讶到他们的功能,就像“我怎么就没发现”这种功能. 1. Peek View(本地查看程序源代码位置,便捷跳转) 可以在不 ...

  7. 3:C#异步WaitAll的使用

    编写界面如图: private async void button1_Click(object sender, EventArgs e) { #region 单个执行的异步,效率慢 HttpClien ...

  8. .NET Core调用WCF的最佳实践

    现在.NET Core貌似很火,与其他.NET开发者交流不说上几句.NET Core都感觉自己落伍了一样.但是冷静背后我们要也看到.NET Core目前还有太多不足,别的不多说,与自家的服务框架WCF ...

  9. WEB文本框提示

    <input type="text" placeholder="文本框提示语" name="version_no"/>

  10. dynamic的一些使用心得

    dynamic关键字才出来的时候,觉得真是没什么用,谁总是和com交互来交互去啊,唯恐避之不及啊. 后来逐渐算是有了一些使用心得,发现这货还真是犀利啊,故在此举几个例子,起抛砖引玉之用. 1.替代XX ...