python装饰器入门
按别人的教程弄的。
要清楚基于类和基于函数的实现的不同之处。
#!/usr/bin/env python # -*- coding: utf-8 -*- ''' class entryExit(object): def __init__(self, f): self.f = f def __call__(self): print "Enering", self.f.__name__ self.f() print "Exited", self.f.__name__ def entryExit(f): def new_f(): print "Entering", f.__name__ f() print "Exited", f.__name__ return new_f @entryExit def func1(): print "inside func1()" @entryExit def func2(): print "inside func2()" func1() func2() class decoratorWithoutArguments(object): def __init__(self, f): print "Inside __init__()" self.f = f def __call__(self, *args): print "Inside __call__()" self.f(*args) print "After self.f(*args)" @decoratorWithoutArguments def sayHello(a1, a2, a3, a4): print 'sayHello arguments:', a1, a2, a3, a4 print "After decoration" print "Preparing to call sayHello()" sayHello("say", "hello", "argument", "list") print "After first sayHello() call" sayHello("a", "different", "set of", "arguments") print "After second sayHello() call" class decoratorWithArguments(object): def __init__(self, arg1, arg2, arg3): print "Inside __init__()" self.arg1 = arg1 self.arg2 = arg2 self.arg3 = arg3 def __call__(self, f): print "Inside __call__()" def wrapped_f(*args): print "Inside wrapped_f()" print "Decorator arguments:", self.arg1, self.arg2, self.arg3 f(*args) print "After f(*args)" return wrapped_f @decoratorWithArguments("hello", "world", 42) def sayHello(a1, a2, a3, a4): print 'sayHello arguments:', a1, a2, a3, a4 print "After decoration" print "Preparing to call sayHello()" sayHello("say", "hello", "argument", "list") print "After first sayHello() call" sayHello("a", "different", "set of", "arguments") print "After second sayHello() call" ''' def decoratorFunctionWithArguments(arg1, arg2, arg3): def wrap(f): print "Inside wrap()" def wrapped_f(*args): print "Inside wrapped_f()" print "Decorator arguments:", arg1, arg2, arg3 f(*args) print "After f(*args)" return wrapped_f return wrap @decoratorFunctionWithArguments("Hello", "world", 42) def sayHello(a1, a2, a3, a4): print 'sayHello arguments:', a1, a2, a3, a4 print "After decoration" print "Preparing to call sayHello()" sayHello("say", "hello", "argument", "list") print "After first sayHello() call" sayHello("a", "different", "set of", "arguments") print "After second sayHello() call"
python装饰器入门的更多相关文章
- Python 装饰器入门(上)
翻译前想说的话: 这是一篇介绍python装饰器的文章,对比之前看到的类似介绍装饰器的文章,个人认为无人可出其右,文章由浅到深,由函数介绍到装饰器的高级应用,每个介绍必有例子说明.文章太长,看完原文后 ...
- Python 装饰器入门(下)
继续上次的进度:https://www.cnblogs.com/flashBoxer/p/9847521.html 正文: 装饰类 在类中有两种不通的方式使用装饰器,第一个和我们之前做过的函数非常相似 ...
- python -- 装饰器入门
用例: 统计函数执行需要的时间 假设我们执行的一段代码的运行时间比我们预想的时间要久,而这段代码块有多个函数调用组成,我们有理由相信至少是其中的一个函数调用导致整个代码块产生了瓶颈.我们如何去发现导致 ...
- Python 装饰器学习
Python装饰器学习(九步入门) 这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方式. 第一步:最简单的函数,准备附加额外功能 1 2 3 4 5 6 7 8 # -*- c ...
- Python装饰器与面向切面编程
今天来讨论一下装饰器.装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等.装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量函数中与函数 ...
- python装饰器总结
一.装饰器是什么 python的装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象.简单的说装饰器就是一个用来返回函数的函数 ...
- Python装饰器学习
Python装饰器学习(九步入门) 这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方式. 第一步:最简单的函数,准备附加额外功能 ? 1 2 3 4 5 6 7 8 # -*- ...
- 【转】Python装饰器与面向切面编程
原文请参考: http://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html 今天来讨论一下装饰器.装饰器是一个很著名的设计模式,经常被用于有切 ...
- Python装饰器探险
关于python装饰器的理解和用法,推荐廖雪峰老师和这一篇博客以及知乎 以下代码均已手动敲过,看完本篇内容,包你装饰器小成! 装饰器实际上就是为了给某程序增添功能,但该程序已经上线或已经被使用,那么就 ...
随机推荐
- PHPExcel类的使用讲解
下面是总结的几个使用方法 include 'PHPExcel.php'; include 'PHPExcel/Writer/Excel2007.php'; //或者include 'PHPExcel/ ...
- QT5笔记:关闭应用程序和窗口的函数
23333 QT一坨,求一门面向傻瓜的语言. QT中 quit(),exit()以及close():常用的三个槽 对主程序的退出,可以调用成员函数exit(),同时也可以调用槽quit(),二者此 ...
- Calico在Docker中的搭建
一,Multi-host网络需求 开始之前推荐两篇文章 http://xelatex.github.io/2015/11/15/Battlefield-Calico-Flannel-Weave-and ...
- JavaScript——this关键字
请看下面的代码,最后alert出来的是什么呢? 1 var name = "Bob"; 2 var nameObj ={ 3 name : "Tom", 4 s ...
- [20160731]read a file and print it on the screen
//read a file and print it on the screen import java.io.*; public class MyPrintStreamTest2{ public s ...
- Window 下 Qt5 使用QMediaplayer 进行视频播放 流播放问题
int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget *widget = new QWidget; widget ...
- 解读Unity中的CG编写Shader系列六(漫反射)
转自 http://www.itnose.net/detail/6116553.html 如果前面几个系列文章的内容过于冗长缺乏趣味着实见谅,由于时间原因前面的混合部分还没有写完,等以后再补充,现在开 ...
- 100m和1000m网线的常见制作方法
100m和1000m网线的常见制作方法 100m和1000m网线的常见制作方法: 5类线(100m)的制作: a: 绿白(3).绿(6).橙白(1).蓝(4).蓝白(5).橙(2).棕白(7).棕(8 ...
- Java for LeetCode 234 Palindrome Linked List
解题思路: O(1)的空间复杂度,意味着不能通过开一个List来解决问题.我们可以把List分成前后两个部分,后半部分通过指针的相互赋值进行翻转即可. JAVA实现如下: public static ...
- hdu 1002.A + B Problem II 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目意思:就是大整数加法. 两年几前做的,纯粹是整理下来的. #include <stdi ...