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. RAID简述

    RAID:Redundant Arrays of Independent Disks(独立冗余磁盘阵列) ①磁盘阵列是由很多价格较便宜的磁盘,组合成一个容量巨大的磁盘组,利用个别磁盘提供数据所产生加成 ...

  2. AS错误:Error:Execution failed for task ':gM99SDK:processReleaseResources'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'F:\BaiduYunDown

    原因,buildToolsVersion 版本太低. 在build.gradle文件设置 buildToolsVersion 设置高一点,但必须是SDK里面有的.

  3. oracle数据学习第一天

    SQL(Strutured Query Language):结构化查询语言 SQL可分为: <1>数据定义语言(DDL):Data Definition Language 用于建立.修改. ...

  4. [python] 线程池

    特别感谢simomo 什么是线程池? 诸如web服务器.数据库服务器.文件服务器和邮件服务器等许多服务器应用都面向处理来自某些远程来源的大量短小的任务.构建服务器应用程序的一个过于简单的模型是:每当一 ...

  5. 乐视云计算基于OpenStack的IaaS实践

    本文作者岳龙广,现在就职于乐视云计算有限公司,负责IaaS部门的工作. 从开始工作就混在开源世界里,在虚拟化方面做过CloudStack/Ovirt开发,现在是做以OpenStack为基础的乐视云平台 ...

  6. PHP查看当前端口号

    <?php echo "当前页面服务器IP地址为:"; echo $_SERVER["SERVER_ADDR"]; echo "<br / ...

  7. php总体架构图

  8. 2748: [HAOI2012]音量调节

    Description 一个吉他手准备参加一场演出.他不喜欢在演出时始终使用同一个音量,所以他决定每一首歌之前他都要改变一次音量.在演出开始之前,他已经做好了一个列表,里面写着在每首歌开始之前他想要改 ...

  9. Git命令行初体验

    1. git 版本控制系统 ==============运行环境======== 系统:windows git : Git-1.7.3.1-preview20101002.rar  下载地址:http ...

  10. Spring的定时任务配置(转)

    spring的定时任务配置分为三个步骤: 1.定义任务 2.任务执行策略配置 3.启动任务 1.定义任务 <!--要定时执行的方法--> <bean id="testTas ...