heading python decorator
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.
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
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
# 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
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.
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
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的更多相关文章
- Python Decorator 和函数式编程
看到一篇翻译不错的文章,原文链接: Python Decorator 和函数式编程
- python decorator的理解
一.decorator的作用 装饰器本质上是一个Python函数,可以让其他函数在不做任何代码变动的前提下增加额外功能. 装饰器的返回值也是一个函数对象.python里函数也是对象. 它经常用于有切面 ...
- 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* ...
- 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 ...
- 两个小例子彻底明白python decorator
一:没有什么实际意思,就是单纯的理解decorator.使用装饰器完全可以阻止方法中的代码执行. class json_test(object): def __init__(self, *arg, * ...
- Python Decorator分析
decorator本身是一个函数,这个函数的功能是接受被修饰的函数(decorated)作为参数,返回包装函数(wrapper)替换被修饰函数(decorated). @decorator func ...
随机推荐
- 解决Debian系统的Crontab执行时间时差问题
首先用 * * * * * date >> /root/log.log 做个测试,发现显示的是UTC的时间,但是直接执行date,得到的是CST的时间.可见在Debian里crontab的 ...
- ArcGIS api fo silverlight学习一(silverlight加载GeoServer发布的WMS地图)
最好的学习资料ArcGIS api fo silverlight官网:http://help.arcgis.com/en/webapi/silverlight/samples/start.htm 一. ...
- lambda匿名函数
1.python中的匿名函数的格式 lambda arg1,arg2...,argN:expression (lambda关键字后,冒号":"前是参数,多个参数用逗号&qu ...
- C#绘制传感器代码
//以下代码添加到任一窗口下即可 private int 旋转角度 = 0; private int 边长 = 10; protected override ...
- Josn序列化与反序列化
using System.Web.Script.Serialization; /// <summary> /// 序列化器 /// </summary&g ...
- 多层数据库应用基于Delphi DataSnap方法调用的实现(一)返回数据集
从Delphi 2009开始,DataSnap技术发生了很大的变化,并在Delphi 2010和Delphi XE的后续版本中得到了持续的改进.Delphi 2009之前的DataSnap,虽然也实现 ...
- win7默认网关不可用怎么解决
方法一:自动获取 1 有的电脑设置了固定的网关和IP地址. 设置方法: 进入"控制面板" , 然后点击"网络和Internet"!! 步骤阅读 2 然后点击& ...
- java中的那些坑
最近准备换工作,为了少让人家鄙视,就要狠狠地藐视这些面试题目.找了本电子书,发了有好多坑,都是特别简单,但是很少有人做对的题目.面对这样的题目,我却有一种兴奋的感觉,也许是因为一直做着重复的工作没有新 ...
- Linux驱动学习之驱动开发准备工作
一.开启驱动开发之路 1.驱动开发的准备工作 (1)正常运行linux系统的开发板.要求开发板中的linux的zImage必须是自己编译的,不能是别人编译的.原因在于在安装模块的时候会进行安全性校验 ...
- Linuxmint&win7
一.选择好Linux发行版,并下载ISO镜像.Linux发行版众多,然而主流的无非Linuxmint.Ubuntu.Fedora.Debian等,当然国内的Deepin也不错.小编旧文已专门就&quo ...