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 ...
随机推荐
- Android permission
1. users-permission Users-permission is the permission that this app should acquire, so that the app ...
- JSP中显示用户信息
<%@ page language= "java" contentType="text/html;charset=UTF-8" %><%@ p ...
- jquery :checked(过滤选择器) 和 空格:checked(后代选择器)
jquery 过滤选择器 和 后代选择器 <%@ page language="java" contentType="text/html; charset=UTF- ...
- jsp学习--如何定位错误和JSP和Servlet的比较
一.如何查找JSP页面中的错误 JSP页面中的JSP语法格式有问题,导致其不能被翻译成Servlet源文件,JSP引擎将提示这类错误发生在JSP页面中的位置(行和列)以及相关信息.JSP页面中的JS ...
- 单片机TM4C123学习(九):PWM
1.头文件与变量定义 #include "tiva_pwm.h" // PWM 2.初始化 // PWM 初始化,频率为1000,占空比为0 M1PWM7_init(, ); // ...
- iOS AFNetworking “Request failed: unacceptable content-type: text/html”问题
使用AFNetworking出现报错: error=Error Domain=com.alamofire.error.serialization.response Code=-1016 "R ...
- [vb.net]判断窗体是否已打开
1.使用OpenForms if my.Application.OpenForms.Item("FormName") isnot nothing then搜索 do somethi ...
- JavaScript-创建新数组
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- BZOJ4293 [PA2015]Siano(线段树)
传送门 这Seg确实不好写,不过因为它与ai的相对顺序无关,所以,我们在对ai排序之后,就可做了.维护一个区间最大值,维护一个和,维护一个区间赋值的懒标记,再维护一个时间变化的标记就可以了. 因为不论 ...
- Python成长笔记 - 基础篇 (四)函数
1.面向对象:类(class) 2.面向过程:过程(def) 3.函数式编程:函数(def)----python 1.函数:http://egon09.blog.51cto.com/9161406 ...