python函数装饰器(decorators)
来源:互联网 发布:韩式四宫格拼图软件 编辑:程序博客网 时间:2024/05/16 11:53
原文出处:http://thecodeship.com/patterns/guide-to-python-function-decorators/
在本文中我们主要介绍什么是函数装饰器?如何使用它?
1、关于函数我们需要知道这些
在正式进入装饰器之前,我们需要知道python中函数都可以干嘛。
可以将函数赋给变量:
def greet(name): return "hello "+namegreet_someone = greetprint greet_someone("John")
可以在函数内部再定义函数:
def greet(name): return "hello "+namegreet_someone = greetprint greet_someone("John")
可以将函数作为参数传递:
def greet(name): return "Hello " + namedef call_func(func): other_name = "John" return func(other_name) print call_func(greet)
可以将函数作为参数返回:
def compose_greet_func(): def get_message(): return "Hello there!" return get_messagegreet = compose_greet_func()print greet()
2、函数装饰器
函数装饰器就是将以上所提到的全部放在一起:
def get_text(name): return "lorem ipsum, {0} dolor sit amet".format(name)def p_decorate(func): def func_wrapper(name): return "<p>{0}</p>".format(func(name)) return func_wrappermy_get_text = p_decorate(get_text)print my_get_text("John")
3、python中的函数装饰器
当然,在python中我们不需要通过"my_get_text = p_decorate(get_text)"来获取装饰器,python提供了专门用于装饰器的语法,我们只需要使用@操作符告诉python解释器我们使用什么函数来装饰我们的函数即可:
def p_decorate(func): def func_wrapper(name): return "<p>{0}</p>".format(func(name)) return func_wrapper@p_decoratedef get_text(name): return "lorem ipsum, {0} dolor sit amet".format(name)print get_text("John")
我们还可以使用多个装饰器来装饰一个函数:
def p_decorate(func): def func_wrapper(name): return "<p>{0}</p>".format(func(name)) return func_wrapperdef strong_decorate(func): def func_wrapper(name): return "<strong>{0}</strong>".format(func(name)) return func_wrapperdef div_decorate(func): def func_wrapper(name): return "<div>{0}</div>".format(func(name)) return func_wrapper@div_decorate@p_decorate@strong_decoratedef get_text(name): return "lorem ipsum, {0} dolor sit amet".format(name)print get_text("John")
我们也可以用装饰器来装饰类方法:
def p_decorate(func): def func_wrapper(*args, **kwargs): return "<p>{0}</p>".format(func(*args, **kwargs)) return func_wrapperclass Person(object): def __init__(self): self.name = "John" self.family = "Doe" @p_decorate def get_fullname(self): return self.name+" "+self.familymy_person = Person()print my_person.get_fullname()
装饰器还可以有自己的参数:
def tags(tag_name): def tags_decorator(func): def func_wrapper(name): return "<{0}>{1}</{0}>".format(tag_name, func(name)) return func_wrapper return tags_decorator@tags("p")def get_text(name): return "Hello "+nameprint get_text("John")
以上实现的装饰器会改变函数对象本身的属性,如:
print get_text.__name__
以上调用的输出是“func_wrapper",事实上get_text的函数对象的__name__,__doc__,__mudole__等都被装饰函数覆盖了,为了不被覆盖,我们可以使用functools.wraps这个装饰器。如:
from functools import wrapsdef tags(tag_name): def tags_decorator(func): @wraps(func) def func_wrapper(name): return "<{0}>{1}</{0}>".format(tag_name, func(name)) return func_wrapper return tags_decorator@tags("p")def get_text(name): """returns some text""" return "Hello "+nameprint get_text.__name__ # get_textprint get_text.__doc__ # returns some textprint get_text.__module__ # __main__
本文仅仅只是简单的装饰器介绍,更详尽的内容可以参考:https://wiki.python.org/moin/PythonDecoratorLibrary
0 0
- python函数装饰器(decorators)
- python装饰器Decorators
- Python Decorators 装饰器
- (翻译) draft.js Decorators 装饰器
- 装饰器 Decorators
- 装饰器(Decorators)
- Python guide 2.1.5 装饰器(Decorators) 2.1.6动态绑定(Dynamic typing)
- python 的函数Decorators
- SSH2装饰器的decorators.xml
- PYTHON 函数装饰器
- python 函数装饰器
- Python函数:装饰器
- Python 函数装饰器
- python函数装饰器
- python函数装饰器
- python 装饰器函数
- Python Decorators入门 (一)
- Python Decorators入门 (一)
- 安装卸载openjdk&&sunjdk
- javaSE(11)(集合迭代器与比较器)
- hpuoj--校赛--特殊的比赛日期(素数判断+模拟)
- ThinkPHP框架二 框架配置
- html语义化
- python函数装饰器(decorators)
- BBS项目总结!
- iOS——Core Graphic框架学习笔记
- Cloud Design Pattern - Throttling Pattern(节流模式)
- 元素类型为 "class" 的内容必须匹配 "(meta*
- Http
- hpuoj--校赛--考试来了(水题)
- CodeForces 496C-- Removing Columns-暴力
- Perf -- Linux下的系统性能调优工具介绍