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的更多相关文章

  1. Python Decorator 和函数式编程

    看到一篇翻译不错的文章,原文链接: Python Decorator 和函数式编程

  2. 使用国内镜像通过pip安装python的一些包 Cannot fetch index base URL http://pypi.python.org/simple/

    原文地址:http://www.xuebuyuan.com/1157602.html 学习flask,安装virtualenv环境,这些带都ok,但是一安装包总是出错无法安装, 比如这样超时的问题: ...

  3. pip安装python包出现Cannot fetch index base URL http://pypi.python.org/simple/

    pipinstall***安装python包,出现 Cannot fetch index base URL  http://pypi.python.org/simple /错误提示或者直接安装不成功. ...

  4. 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 ...

  5. python decorator的理解

    一.decorator的作用 装饰器本质上是一个Python函数,可以让其他函数在不做任何代码变动的前提下增加额外功能. 装饰器的返回值也是一个函数对象.python里函数也是对象. 它经常用于有切面 ...

  6. python decorator 基础

    一般来说,装饰器是一个函数,接受一个函数(或者类)作为参数,返回值也是也是一个函数(或者类).首先来看一个简单的例子: # -*- coding: utf-8 -*- def log_cost_tim ...

  7. Python decorator

    1.编写无参数的decorator Python的 decorator 本质上就是一个高阶函数,它接收一个函数作为参数,然后,返回一个新函数. 使用 decorator 用Python提供的 @ 语法 ...

  8. python decorator的本质

    推荐查看博客:python的修饰器 对于Python的这个@注解语法糖- Syntactic Sugar 来说,当你在用某个@decorator来修饰某个函数func时,如下所示: @decorato ...

  9. Python decorator装饰器

    问题: 定义了一个新函数 想在运行时动态增加功能 又不想改动函数本身的代码 通过高阶段函数返回一个新函数 def f1(x): return x*2 def new_fn(f): #装饰器函数 def ...

随机推荐

  1. AndroidStudio调试APP

    AndroidStudio调试APP 电脑用数据线连上手机 开启手机的开发者模式 注:通常连续狂点手机的版本号即可显示出开发者选项,然后进入菜单,勾选开发者选项.USB调试. 给程序加断点 单击调试按 ...

  2. Linux程序包管理初步-yum的使用

    何为yum? yum:由yellow dog研发,全称Yellowdog Update Modifier 是rhel系列系统上rpm包管理器的前端工具,可用来自动解决依赖关系,从而更好的实现程序包的安 ...

  3. 超链接的那些事(二): 属性href

    a标签的属性之一 href 1. 定义 href 属性用于指定超链接目标的 URL. 2. 用法     ①. 锚点 同一页面添加锚点 (1)<a href="#test"& ...

  4. AESwithJCE http://www.coderanch.com/how-to/content/AES_v1.html

    Using AES with Java Technology By Rags SrinivasJune 2003 In September 2000, the National Institute o ...

  5. 技术英文单词贴--S

    S separator 分离,隔离器 shortcut 快捷方式,捷径 sort 排序 special 特殊的,专用的 specified 规定的,详细说明,指定 specify 指定,明确提出,详细 ...

  6. java核心知识点学习----equals和==的比较、单例模式,饿汉式,饱汉式

    最近发现自己学习能力变慢了,想来想去还是发现是因为自己Java基础没有打扎实,接下来的一系列文章将主要记录自己对于Java的最基础知识点的学习. 一.equals和==的比较 先看例子: packag ...

  7. [Notes] Reading Notes on [Adaptive Robot Control – mxautomation J. Braumann 2015]

    Reading sources: 1.Johannes Braumann, Sigrid Brell-Cokcan, Adaptive Robot Control (ARC  ) Note: buil ...

  8. XE3随笔6:SuperObject 的 JSON 对象中还可以包含 "方法"

    SuperObject 的 JSON 对象中还可以包含 "方法", 这太有意思了; 其方法的格式是: procedure Method(const This, Params: IS ...

  9. CardView的简单介绍

    CardView是Android5.0中的一个全新控件,本质上而言,CardView是一个增加了圆角和阴影效果的FrameLayout,没错它就是一个FrameLayout,一个布局.CardView ...

  10. bugly使用

    导入: 1.Bugly.framework 2.Security.framework 3.SystemConfiguration.framework 4.libc++.1.dylib 5.libz.1 ...