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 ...
随机推荐
- C#操作access数据库
未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序解决办法 去http://download.microsoft.com/download/7/0/3/703ffbcb- ...
- gb2312
---恢复内容开始--- 字符集 gb2312(字全)采用2两个字节(一个汉字) utf-8(只有汉字)采用3个字节(一个汉字) html就是负责描述页面的语义:css负责描述页面的样式:js负责描述 ...
- spring代理模式 service远程调用,插件执行
最近,研究了一下平台远程调用的过程,和service层插件执行的原理,记录一下. 1.远程service调用过程 首先看一下类的继承结构 封装调用处理过程 封装service调用接口 封装servic ...
- nmon工具的安装及简单使用
1.工具的安装 下载rpm包安装即可http://mirror.ghettoforge.org/distributions/gf/el/6/gf/x86_64/nmon-14i-1.gf.el6.x8 ...
- 用sass写栅格系统
为了验证学习sass的效果,自己写了个简单的栅格系统.
- linux 学习8 权限管理
第八章 权限管理 8.1 ACL权限 8.2 文件特殊权限 8.3 文件系统属性chattr权限 8.4 系统命令sudo权限 8.1 ACL权限 ACL权限简介与开启 查看与设定ACL权限 最大有效 ...
- ANdroid5.0不能隐式启动service,必须显示,解决办法,加服务端包名
Intent intent = new Intent(); intent.setAction("com.viaembedded.veonvif.RemoteService");// ...
- dfs 翻棋盘end
#include<iostream> char data[16]; int a[16]; int d[4] = { -4, 1, 4, -1 }; char b[16]; int flag ...
- reference
z@z:~$ g++ -std=c++11 ref.cpp ref.cpp:7:10: error: invalid conversion from 'int' to 'int*' [-fpermis ...
- iOS有用的三方库
DKNightVersion https://github.com/Draveness/DKNightVersion#podfile 用来为APP添加夜间模式和换肤功能