decorator make a wrapper function do something before and after the original function. The wrapper function share arguments with original function.@decorator is same with func = decorator(func); decorator receive func and return a wrapper func with same arguments with original func. The decorator function is done in @decorator progress. Wrapper fucntion in decorator share the same argument with original function and run the original function in itself. If you want decotrator receive some arguements(as decorator receiv function arguemnt return arguments funcion), you need the decotrator return a maker function which receive argument and return wrapper function(same arguments with original function). The sample is as follows.

  • so a maker functions will return wrapper function with arguments same with original fucntion.
  • We user maker functions to receive arguments for wrapper functions. So wrapper functions can do something with some other arguments.
In [67]:
def mydecorator_maker(*args, **kwargs):
print "this is decorator maker function..."
print args, kwargs
def mydecorator(func):
print "this is decorator... args is passed here"
print args, kwargs
def decorator_wrapper(func_arg1, func_arg2):
print "this is decorator wrapper, before function..."
print "func_arg1 " + func_arg1 + " func_arg2 " + func_arg2
func(func_arg1, func_arg2)
print "this is decorator wrapper, after function"
return decorator_wrapper
return mydecorator @mydecorator_maker(1, 2, 3, test="test")
def mydecorated_function(func_arg1, func_arg2):
print "this is decorated function with arg1 " + func_arg1 + " arg2 " + func_arg2
 
this is decorator maker function...
(1, 2, 3) {'test': 'test'}
this is decorator... args is passed here
(1, 2, 3) {'test': 'test'}
 

This is another example for decorated doecorator. It works also with maker and with interation. Nothing special from above maker function

In [1]:
def decorator_with_args(decorator_to_enhance):
"""
This function is supposed to be used as a decorator.
It must decorate an other function, that is intended to be used as a decorator.
Take a cup of coffee.
It will allow any decorator to accept an arbitrary number of arguments,
saving you the headache to remember how to do that every time.
""" # We use the same trick we did to pass arguments
def decorator_maker(*args, **kwargs): # We create on the fly a decorator that accepts only a function
# but keeps the passed arguments from the maker.
def decorator_wrapper(func): # We return the result of the original decorator, which, after all,
# IS JUST AN ORDINARY FUNCTION (which returns a function).
# Only pitfall: the decorator must have this specific signature or it won't work:
return decorator_to_enhance(func, *args, **kwargs) return decorator_wrapper return decorator_maker
In [2]:
# You create the function you will use as a decorator. And stick a decorator on it :-)
# Don't forget, the signature is "decorator(func, *args, **kwargs)"
@decorator_with_args
def decorated_decorator(func, *args, **kwargs):
def wrapper(function_arg1, function_arg2):
print "Decorated with", args, kwargs
return func(function_arg1, function_arg2)
return wrapper # Then you decorate the functions you wish with your brand new decorated decorator. @decorated_decorator(42, 404, 1024)
def decorated_function(function_arg1, function_arg2):
print "Hello", function_arg1, function_arg2 decorated_function("Universe and", "everything")
#outputs:
#Decorated with (42, 404, 1024) {}
#Hello Universe and everything # Whoooot!
 
Decorated with (42, 404, 1024) {}
Hello Universe and everything
In [3]:
decorated_decorator = decorator_with_args(decorated_decorator)
 

I will use a decorated docorator to receive arguments instend of maker functions. I think this is a really realization of intetraion.

  • Decorator means a function change from orginal function to wrapper function in decorator function.
  • The decorater function here will be change to a function do something with arguments and return the deocrator(which change original fucntion to wrapper).
  • We can make this change by a decorated decorator.
  • The decorated decorator receive decorator return a enhancer function dothing something with arguments. The function return the decorator at last. The enchancer function provide a enhacement to decorator with ablity to do something with some arguments.
In [71]:
def decorator_enhance_decorator(decorator_to_enhancement):
print "This decorated decorator will enhance decorator function..."
print decorator_to_enhancement
#def decorator_enhancer(*args, **kwargs):
def decorator_enhancer(*args, **kwargs):
print "decorator enhancer do something with args..."
print args
print kwargs
return decorator_to_enhancement
return decorator_enhancer
#return decorator_enhance_maker @decorator_enhance_decorator
def decorated_decorator3(func):
print func
print "func is decorated here"
def wrapper(func_arg1, func_arg2):
print "wrapper with arg1 " + func_arg1 + " and arg2 " + func_arg2
return func(func_arg1, func_arg2)
return wrapper @decorated_decorator3(1, 2, 3, test="test")
def decorated_function3(func_arg1, func_arg2):
print "decorated function with func arg1 " + func_arg1 + " arg2" + func_arg2
print "do something in decorated function" decorated_function3("Liliy", "Baby")
 
This decorated decorator will enhance decorator function...
<function decorated_decorator3 at 0x10b7755f0>
decorator enhancer do something with args...
(1, 2, 3)
{'test': 'test'}
<function decorated_function3 at 0x10b775668>
func is decorated here
wrapper with arg1 Liliy and arg2 Baby
decorated function with func arg1 Liliy arg2Baby
do something in decorated function
In [47]:
mydecorated_function("liliy", "funny")
 
this is decorator wrapper, before function...
func_arg1 liliy func_arg2 funny
this is decorated function with arg1 liliy arg2 funny
this is decorator wrapper, after function

heading python decorator的更多相关文章

  1. Python Decorator 和函数式编程

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

  2. python decorator的理解

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

  3. 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* ...

  4. python decorator 基础

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

  5. Python decorator

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

  6. python decorator的本质

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

  7. Python decorator装饰器

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

  8. 两个小例子彻底明白python decorator

    一:没有什么实际意思,就是单纯的理解decorator.使用装饰器完全可以阻止方法中的代码执行. class json_test(object): def __init__(self, *arg, * ...

  9. Python Decorator分析

    decorator本身是一个函数,这个函数的功能是接受被修饰的函数(decorated)作为参数,返回包装函数(wrapper)替换被修饰函数(decorated). @decorator func ...

随机推荐

  1. IIS7 IIS8 中多个版本php共存的方法

    原文地址: https://blog.cozof.com/pieces/54.shtml 最近又重回.net,用回IIS.然后用到某个php开源项目,需要低版本的php,之前装的一个php5.5不能用 ...

  2. 移动端自动化环境搭建-RIDE的安装

    A.安装依赖 RIDE 是 Robot Framework 测试数据的编辑器.它使测试用例的创建.运行.测试项目的组织可以在图形界面下完成. B.安装过程 下载地址:https://pypi.pyth ...

  3. Flex使用Blazeds与Java交互及自定义对象转换详解-DATAGRID读取ORACLE数据

    http://www.cnblogs.com/RocD-DuPeng/articles/1751040.html 一.建立Flex与Java交互的工程. 本文中讲到的交互是利用Blazeds的,因为这 ...

  4. cmd中用PING命令时,出现'Ping' 不是内部或外部命令 解决方案

    在cmd中用PING命令时,出现'Ping' 不是内部或外部命令,也不是可运行的程序或批处理文件.先了解一下内容:1.可执行文件.命令文件和批处理文件以.exe或者.com或者.bat为扩展名的文件分 ...

  5. Nginx 和 PHP的安装配置

    1.安装PHP(注意系统默认安装了php,要安装高版本之前最好卸载旧版本,因为这个原因纠结了四个小时) ./configure \ --prefix=/usr/local/php \ --exec-p ...

  6. Linux和windows之间通过scp复制文件

    Windows是不支持ssh协议的 需要安装WinSSHD 安装以及设置过程如下: BvSshServer(原名winsshd)官方下载页在这里:https://www.bitvise.com/dow ...

  7. RabbitMQ 入门指南(Java)

    RabbitMQ是一个受欢迎的消息代理,通常用于应用程序之间或者程序的不同组件之间通过消息来进行集成.本文简单介绍了如何使用 RabbitMQ,假定你已经配置好了rabbitmq服务器. Rabbit ...

  8. Nginx使用Expires增加浏览器缓存加速

    Max-age是指我们的web中的文件被用户访问(请求)后的存活时间,是个相对的值,相对Request_time(请求时间). Expires它比max-age要麻烦点,Expires指定的时间分&q ...

  9. scala-尾递归

    ------------------------- by chenkh ----------------------------- 随笔记录什么是尾递归,为什么需要尾递归,尾递归show by exa ...

  10. 【原】如何在jQuery中实现闭包

    原生JS中,闭包虽好用,但是很难用好,在jQuery中一样,都有一些点需要我们注意.jQuery中使用闭包的常见情况有以下几种: 1.$(document).ready()的参数 我们在写jQuery ...