Python3.5装饰器典型案例分析

本文实例讲述了Python3.5装饰器。分享给大家供大家参考,具体如下:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
#高阶函数+嵌套函数==>装饰器
import time
def timer(func):    #timer(test1)-->func=test1
  def decor():
    start_time = time.time()
    func()     #run test1
    stop_time = time.time()
    print("the run time of func is %s" %(stop_time-start_time))
  return decor
@timer   #test1 = timer(test1)
def test1():
  time.sleep(3)
  print("in the test1")
@timer   #test2 = timer(test2)
def test2():
  time.sleep(3)
  print("in the test2")
print(timer(test1))     #打印deco的地址
#test1 = timer(test1)
#test2 = timer(test2)
test1()  #-->执行decor
test2()

运行结果:

<function timer.<locals>.decor at 0x00B720C0>
in the test1
the run time of func is 3.000171661376953
in the test2
the run time of func is 3.000171661376953

1、装饰器修饰有参数函数

#高阶函数+嵌套函数==>装饰器
import time
def timer(func):    #timer(test1)-->func=test1
  def decor(arg1,arg2):
    start_time = time.time()
    func(arg1,arg2)     #run test2
    stop_time = time.time()
    print("the run time of func is %s" %(stop_time-start_time))
  return decor
@timer   #test2 = timer(test2) = decor  test2(name)==>decor(name)
def test2(name,age):
  print("test2:",name,age)
test2("liu",23)

运行结果 :

test2: liu 23
the run time of func is 0.0

2、装饰器修饰多个函数,有的函数带参数,有的函数不带参数的情况(采用参数组)

#高阶函数+嵌套函数==>装饰器
import time
def timer(func):    #timer(test1)-->func=test1
  def decor(*args,**kwargs):
    start_time = time.time()
    func(*args,**kwargs)     #run test1
    stop_time = time.time()
    print("the run time of func is %s" %(stop_time-start_time))
  return decor
@timer   #test1 = timer(test1)
def test1():
  time.sleep(3)
  print("in the test1")
@timer   #test2 = timer(test2) = decor  test2(name)==>decor(name)
def test2(name,age):
  time.sleep(1)
  print("test2:",name,age)
#test1 = timer(test1)
#test2 = timer(test2)
test1()  #-->执行decor
test2("liu",23)

运行结果:

in the test1
the run time of func is 3.0036065578460693
test2: liu 23
the run time of func is 1.0084023475646973

更多关于Python相关内容可查看本站专题:《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

(0)

相关推荐

  • 介绍Python的@property装饰器的用法

    在绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数,导致可以把成绩随便改: s = Student() s.score = 9999 这显然不合逻辑.为了限制score的范围,可以通过一个set_score()方法来设置成绩,再通过一个get_score()来获取成绩,这样,在set_score()方法里,就可以检查参数: class Student(object): def get_score(self): return self._score def set_s

  • python3.6中@property装饰器的使用方法示例

    本文实例讲述了python3.6中@property装饰器的使用方法.分享给大家供大家参考,具体如下: 1.@property装饰器的使用场景简单记录如下: 负责把一个方法变成属性调用: 可以把一个getter方法变成属性,@property本身又创建了另一个装饰器@score.setter,负责把一个setter方法变成属性赋值: 只定义getter方法,不定义setter方法就是一个只读属性 2.通过一个例子来加深对@property装饰器的理解:利用@property给一个Screen对象

  • 浅谈解除装饰器作用(python3新增)

    一个装饰器已经作用在一个函数上,你想撤销它,直接访问原始的未包装的那个函数. 假设装饰器是通过 @wraps 来实现的,那么你可以通过访问 wrapped 属性来访问原始函数: >>> @somedecorator >>> def add(x, y): ... return x + y ... >>> orig_add = add.__wrapped__ >>> orig_add(3, 4) 7 >>> 如果有多个包

  • Python黑魔法@property装饰器的使用技巧解析

    @property有什么用呢?表面看来,就是将一个方法用属性的方式来访问. 上代码,代码最清晰了. class Circle(object): def __init__(self, radius): self.radius = radius @property def area(self): return 3.14 * self.radius ** 2 c = Circle(4) print c.radius print c.area 可以看到,area虽然是定义成一个方法的形式,但是加上@pr

  • python类装饰器用法实例

    本文实例讲述了python类装饰器用法.分享给大家供大家参考.具体如下: #!coding=utf-8 registry = {} def register(cls): registry[cls.__clsid__] = cls return cls @register class Foo(object): __clsid__ = '123-456' def bar(self): pass print registry 运行结果如下: {'123-456': <class '__main__.F

  • Python 使用类写装饰器的小技巧

    最近学到了一个有趣的装饰器写法,就记录一下. 装饰器是一个返回函数的函数.写一个装饰器,除了最常见的在函数中定义函数以外,Python还允许使用类来定义一个装饰器. 1.用类写装饰器 下面用常见的写法实现了一个缓存装饰器. def cache(func): data = {} def wrapper(*args, **kwargs): key = f'{func.__name__}-{str(args)}-{str(kwargs)})' if key in data: result = data

  • python3 property装饰器实现原理与用法示例

    本文实例讲述了python3 property装饰器实现原理与用法.分享给大家供大家参考,具体如下: 学习python的同学,慢慢的都会接触到装饰器,装饰器在python里是功能强大的语法.装饰器配合python的魔法方法,能实现很多意想不到的功能.废话不多说,如果你已经掌握了闭包的原理,代码的逻辑还是可以看明白的,咱们直接进入正题. property的意义 @property把一个类的getter方法变成属性,如果还有setter方法,就在setter方法前面加上@method.setter.

  • Python3.5装饰器原理及应用实例详解

    本文实例讲述了Python3.5装饰器原理及应用.分享给大家供大家参考,具体如下: 1.装饰器: (1)本质:装饰器的本质是函数,其基本语法都是用关键字def去定义的. (2)功能:装饰其他函数,即:为其他函数添加附加功能. (3)原则:不能修改被装饰的函数的源代码,不能修改被装饰的函数的调用方式.即:装饰器对待被修饰的函数是完全透明的. (4)简单应用:统计函数运行时间的装饰器 import time #统计函数运行时间的砖装饰器 def timmer(func): def warpper(*

  • 实例讲解Python编程中@property装饰器的用法

    取值和赋值 class Actress(): def __init__(self): self.name = 'TianXin' self.age = 5 类Actress中有两个成员变量name和age.在外部对类的成员变量的操作,主要包括取值和赋值.简单的取值操作是x=object.var,简单的赋值操作是object.var=value. >>> actress = Actress() >>> actress.name #取值操作 'TianXin' >&g

  • Python类装饰器实现方法详解

    本文实例讲述了Python类装饰器.分享给大家供大家参考,具体如下: 编写类装饰器 类装饰器类似于函数装饰器的概念,但它应用于类,它们可以用于管理类自身,或者用来拦截实例创建调用以管理实例. 单体类 由于类装饰器可以拦截实例创建调用,所以它们可以用来管理一个类的所有实例,或者扩展这些实例的接口. 下面的类装饰器实现了传统的单体编码模式,即最多只有一个类的一个实例存在. instances = {} # 全局变量,管理实例 def getInstance(aClass, *args): if aC

  • Python3.7 新特性之dataclass装饰器

    Python 3.7中一个令人兴奋的新特性是 data classes . 数据类通常是一个主要包含数据的类,尽管实际上没有任何限制. 它是使用新的 @dataclass 装饰器创建的,如下所示: from dataclasses import dataclass @dataclass class DataClassCard: rank: str suit: str 此代码以及本教程中的所有其他示例仅适用于 Python 3.7 及更高版本. 注意: 当然在 Python 3.6 版本也可以使用

随机推荐