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 ...
随机推荐
- wget net-tools
新安装的centos7 minimal 没有安装 wget 需要安装一下,才能安装lnmp yum -y install wget yum -y install net-tools
- RESULT:0x80029C4A (TYPE_E_CANTLOADLIBRARY))
无法将类型为"Microsoft.Office.Interop.Excel.ApplicationClass"的 COM 对象强制转换为接口类型"Microsoft.Of ...
- C语言学习笔记(一)_hello world
一.建立一个文件a.c,写入: #include <stdio.h> //使用printf库函数之前,必须include <stdio.h>int main()//main函数 ...
- html 表格的制作
表格的制作 表格<table></table> <table width="" height="" align=" ...
- datagridview中使用checkbox问题。
如果套用datagridview中的checkboxfield,生成的数据,会出现无法选择datagridview中数据项的问题,即checkbox不可以被鼠标点击,选中/取消选中.此checkbox ...
- Bootstrap 栅格系统(转载)
源地址:http://www.cnblogs.com/linjiqin/p/3559800.html Bootstrap 栅格系统 目录1.简介2.栅格选项3.列偏移4.嵌套列5.列排序 1.简介Bo ...
- docker 安装
Docker使用了一种叫AUFS的文件系统,这种文件系统可以让你一层一层地叠加修改你的文件,最底下的文件系统是只读的,如果需要修改文件,AUFS会增加一个可写的层(Layer),这样有很多好处,例如不 ...
- AsyncTask异步加载和Gson
public class MainActivity extends Activity { private List<Goods> goods=new ArrayList<Goo ...
- three.js 源码注释(三十九)Light/HemisphereLight.js 半球光、 自然光(天光效果)
/*** * HemisphereLight类 是在场景中创建半球光,就是天光效果,经常用在室外,将各个位置的物体都照亮,室内的光线大多是方向性的, * 无论是窗口还是灯槽,用平面光很方便,室外用平面 ...
- 乐视云计算基于OpenStack的IaaS实践
本文作者岳龙广,现在就职于乐视云计算有限公司,负责IaaS部门的工作. 从开始工作就混在开源世界里,在虚拟化方面做过CloudStack/Ovirt开发,现在是做以OpenStack为基础的乐视云平台 ...