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

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

装饰器有其独特的原则: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. ubuntu 出来菜单栏和任务栏

    http://blog.csdn.net/terence1212/article/details/51340595 命令行输入:sudo apt-get install compizconfig-se ...

  2. background image

    http://www.ajaxblender.com/bgstretcher-2-jquery-stretch-background-plugin-updated.html http://blog.d ...

  3. Problem of Uninstall Cloudera: Cannot Add Hdfs and Reported Cannot Find CDH's bigtop-detect-javahome

    1. Problem We wrote a shell script to uninstall Cloudera Manager(CM) that run in a cluster with 3 li ...

  4. spark yarn 集群提交kafka代码

    配置好hadoop的环境,具体根据http://blog.csdn.net/u010638969/article/details/51283216博客所写的进行配置. 运行start-dfs.sh启动 ...

  5. HDU 6065 RXD, tree and sequence (LCA+DP)

    题意:给定上一棵树和一个排列,然后问你把这个排列分成m个连续的部分,每个部分的大小的是两两相邻的LCA的最小深度,问你最小是多少. 析:首先这个肯定是DP,然后每个部分其实就是里面最小的那个LCA的深 ...

  6. B-spline Curves 学习之B样条基函数的定义与性质(2)

    B-spline Basis Functions 本博客转自前人的博客的翻译版本,前几章节是原来博主的翻译内容,但是后续章节博主不在提供翻译,后续章节我在完成相关的翻译学习. (原来博客网址:http ...

  7. weevely入手使用笔记

    -前言 weevely是一款使用python编写的webshell工具,集webshell生成和连接于一身,采用c/s模式构建,可以算作是linux下的一款php菜刀替代工具,具有很好的隐蔽性(生成随 ...

  8. 机器学习—集成学习(Adaboost)

    一.原理部分: 二.sklearn实现: from sklearn.ensemble import AdaBoostClassifier from sklearn.datasets import lo ...

  9. iOS9 Https技术预研

    一.服务器需要做的事情: 1.要注意 App Transport Security 要求 TLS 1.2, 2.而且它要求站点使用支持forward secrecy协议的密码. 3.证书也要求是符合A ...

  10. kubernetes 滚动更新发布及回滚

    基本命令 记录历史 --record kubectl  apply -f **** --record 查看当前状态 kubectl rollout status deployment/demo -w ...