python decorator simple example
Why we need the decorator in python ?
Let's see a example:
#!/usr/bin/python def fun_1(x):
return x*2 def fun_2(x):
return x*x*2 if __name__ == '__main__':
print 'call fun_1'
print fun_1(8)
print 'call fun_2'
print fun_2(9)

We can see more than code heavy:
so we don't want to write the below code any more ...
print 'call fun_1'
print 'call fun_2'
How can we do it with a simple way !
Try use decorator:
#!/usr/bin/python def fun_decorator(f):
def fn(x):
print 'call '+f.__name__
return f(x)
return fn @fun_decorator
def fun_1(x):
return x*2
@fun_decorator
def fun_2(x):
return x*x*2 if __name__ == '__main__':
print fun_1(8)
print fun_2(9)
See the result below:

But,sometime We are expect to write something before or after tips:
something like below,we add some at the head ..

How to make it ?
a simple and stupid method below:
#!/usr/bin/python def fun_decorator(f):
def fn(x):
print 'call '+f.__name__
return f(x)
return fn @fun_decorator
def fun_1(x):
return x*2
@fun_decorator
def fun_2(x):
return x*x*2 if __name__ == '__main__':
print 'INFO',fun_1(8)
print 'DEBUG',fun_2(9)
You can see,we are just simply add some string before call function !
So,How to make it easy ..
#!/usr/bin/python def fun_decorator(user_info):
def wrapper(f):
def fn(x):
print '%s call %s' % (user_info,f.__name__)
return f(x)
return fn
return wrapper @fun_decorator('INFO')
def fun_1(x):
return x*2
@fun_decorator('DEBUG')
def fun_2(x):
return x*x*2 if __name__ == '__main__':
print fun_1(8)
print fun_2(9)

If you function with more than one argument,How can we do ?
#!/usr/bin/python def fun_decorator(user_info):
def wrapper(f):
def fn(x,y):
print '%s call %s' % (user_info,f.__name__)
return f(x,y)
return fn
return wrapper @fun_decorator("INFO")
def fun_1(x,y):
return x*y @fun_decorator("INFO")
def fun_2(x,y):
return x*y*2 if __name__ == '__main__': print fun_1(2,3)
print fun_2(2,3)

Okay,Sometime,we even don't know how many arguments will be input ?
so,How can we do ?
>>>we will answer it next time ! Thank you
python decorator simple example的更多相关文章
- Python Decorator 和函数式编程
看到一篇翻译不错的文章,原文链接: Python Decorator 和函数式编程
- 使用国内镜像通过pip安装python的一些包 Cannot fetch index base URL http://pypi.python.org/simple/
原文地址:http://www.xuebuyuan.com/1157602.html 学习flask,安装virtualenv环境,这些带都ok,但是一安装包总是出错无法安装, 比如这样超时的问题: ...
- pip安装python包出现Cannot fetch index base URL http://pypi.python.org/simple/
pipinstall***安装python包,出现 Cannot fetch index base URL http://pypi.python.org/simple /错误提示或者直接安装不成功. ...
- 46 Simple Python Exercises-Very simple exercises
46 Simple Python Exercises-Very simple exercises 4.Write a function that takes a character (i.e. a s ...
- python decorator的理解
一.decorator的作用 装饰器本质上是一个Python函数,可以让其他函数在不做任何代码变动的前提下增加额外功能. 装饰器的返回值也是一个函数对象.python里函数也是对象. 它经常用于有切面 ...
- python decorator 基础
一般来说,装饰器是一个函数,接受一个函数(或者类)作为参数,返回值也是也是一个函数(或者类).首先来看一个简单的例子: # -*- coding: utf-8 -*- def log_cost_tim ...
- Python decorator
1.编写无参数的decorator Python的 decorator 本质上就是一个高阶函数,它接收一个函数作为参数,然后,返回一个新函数. 使用 decorator 用Python提供的 @ 语法 ...
- python decorator的本质
推荐查看博客:python的修饰器 对于Python的这个@注解语法糖- Syntactic Sugar 来说,当你在用某个@decorator来修饰某个函数func时,如下所示: @decorato ...
- Python decorator装饰器
问题: 定义了一个新函数 想在运行时动态增加功能 又不想改动函数本身的代码 通过高阶段函数返回一个新函数 def f1(x): return x*2 def new_fn(f): #装饰器函数 def ...
随机推荐
- SCI写作经验交流,别人的经验借鉴下,很有用的!
http://www.dxy.cn/bbs/topic/27127771 语言是非英语国家论文的最大障碍.首先是时态和语态:在前言和讨论里,描述该研究的过去历史和现状时,要使用相应的时态:过去就使用过 ...
- struts标签,<s:textfield>嵌套<s:property>的问题
错误:org.apache.jasper.JasperException: /front/orderList.jsp(110,122) equal symbol expected <s:te ...
- VirtualBox中的虚拟机要如何设置,才能够上网
VirtualBox中有4种网络连接方式:1. NAT2. Bridged Adapter3. Internal4. Host-only Adapter 一般设置成NAT网路就可以,但是由于我在公司上 ...
- PHP 数组函数整理
如果你已经使用了一段时间PHP的话,那么,你应该已经对它的数组比较熟悉了——这种数据结构允许你在单个变量中存储多个值,并且可以把它们作为一个集合进行操作. 经常,开发人员发现在PHP中使用这种数据结构 ...
- Linux云服务器安装tomcat
安装tomcat需要安装JDK 1.上传 把下载好的tomcat压缩包(apache-tomcat-7.0.tar.gz)和jdk(jdk-7u76-linux-x64.tar.gz)压缩包上传到/u ...
- python dict
###字典的基本结构 info = { "k1" : "v1", "k2" : "v2" } ###字典的valus可以 ...
- ueditor .net版本上传不了图片问题
百度的Ueditor适合中国人,所以,选择了它,可是恶梦才刚刚开始,最头痛的就是图片死活就是上传不成功,悲剧中.. 现在来分析下为什么报错:什么也不说了,上图
- Java递归输出指定路径下所有文件及文件夹
package a.ab; import java.io.File; import java.io.IOException; public class AE { public static void ...
- JBOSS最大连接数配置和jvm内存配置
一.调整JBOSS最大连接数. 配置deploy/jboss-web.deployer/server.xml文件 . <Connector port="80 ...
- iOS AFNetWorking 下载pdf文档
+ (void)downLoadPdf:(NSString *)url pdf_id:(NSString *)pdf_id block:(APIFilePath)pdfFilePath { NS ...