`

Python's Decorator (Annotation)

阅读更多

python's decorator is like Java's annotation, but more flexible and easy to implement because of python's syntax. Both is to use a clean way to realize AOP, to insert job of other aspect and make you class/func concentrate to bussiness logic.


def anna(fn):
    def new_func(*args):
        print 'by anna args=%s' % args
        return fn(*args)
    return new_func

def annie(ar):
    print 'by annie1 ar=%s' % ar
    def _A(fn):
        def new_func(*args):
            print 'by annie2 args=%s' % args
            return fn(*args)
        return new_func
    return _A
    
class ccc():
    @anna
    def __init__(self):
        print 'ccc'
    @anna
    def ff(self, a):
        print a



@anna
def test1(a):
    print a

@annie('hi')
def test2(a):
    print a

test1((1,2))
test2((3,4))


分享到:
评论

相关推荐

    decorator python(decorator-3.4.0.tar.gz).rar

    这个"decorator python"模块是版本3.4.0的实现,其核心概念是通过函数来包装(即装饰)其他函数,以增强被装饰函数的行为。这个模块可能是由社区成员贡献并分享的,因此它属于"其他资源"类别。 在Python中,装饰器...

    python-decorator-3.4.0-3.el7.noarch.rpm

    离线安装包,亲测可用

    python-decorator-3.0.1-3.1.el6.noarch.rpm

    python-decorator-3.0.1-3.1.el6.noarch

    python实现Decorator模式实例代码

    本文研究的主要是python实现Decorator模式,具体介绍如下。 一般来说,装饰器是一个函数,接受一个函数(或者类)作为参数,返回值也是也是一个函数(或者类)。首先来看一个简单的例子: # -*- coding: utf-8 -*- ...

    python decorator==4.4.2

    在"python decorator==4.4.2"版本中,我们可以探讨这个库提供的装饰器功能及其在Odoo中的应用。 首先,让我们理解什么是Python装饰器。装饰器本质上是一个接收函数作为参数并返回新函数的函数。通过在定义函数前...

    sclo-python35-python-decorator-4.0.11-2.el7.noarch.rpm

    官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装

    sclo-python27-python-decorator-4.0.11-2.el7.noarch.rpm

    官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装

    Python库 | drf_nested_decorator-0.3-py2-none-any.whl

    资源分类:Python库 所属语言:Python 资源全名:drf_nested_decorator-0.3-py2-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    python decorator & closure demo code for learning the use tips

    python decorator & closure demo code for learning the use tips

    python装饰器decorator介绍

    python中的decorator通常为输入一个函数,经过装饰后返回另一个函数。 比较常用的功能一般使用decorator来实现,例如python自带的staticmethod和classmethod。 装饰器有两种形式: 复制代码 代码如下: @A def foo()...

    python中完善decorator共5页.pdf.zip

    Python中的装饰器(Decorator)是该编程语言的一个强大特性,它允许我们修改或增强函数、类等对象的行为,而无需更改其源代码。这个“python中完善decorator共5页.pdf.zip”文件很可能是关于深入理解和高效使用...

    Python中用Decorator来简化元编程的教程

    Decorator 与 Python 之前引入的元编程抽象有着某些共同之处:即使没有这些技术,您也一样可以实现它们所提供的功能。正如 Michele Simionato 和我在 可爱的 Python 专栏的早期文章 中指出的那样,即使在 Python 1.5...

    python中编写无参数decorator共4页.pdf

    Python中的装饰器(Decorator)是高级函数编程的一个重要特性,它允许我们修改或增强其他函数的功能,而无需改动原函数的代码。在Python中,装饰器本质上是一个接收函数作为参数并返回新函数的函数。本资料"python中...

    Python使用logging结合decorator模式实现优化日志输出的方法

    本文实例讲述了Python使用logging结合decorator模式实现优化日志输出的方法。分享给大家供大家参考,具体如下: python内置的loging模块非常简便易用, 很适合程序运行日志的输出。 而结合python的装饰器模式,则可...

    Python装饰器decorator用法实例

    2. **装饰器(Decorator)**: 装饰器是Python中实现装饰模式的一种方式。它是一个可调用的对象,通常是一个函数,接收一个函数作为参数,并返回一个新的函数。装饰器可以用来增强或修改原函数的功能,如日志、性能...

    Python中decorator使用实例

    在Python编程语言中,装饰器(Decorator)是一种强大的工具,用于修改或增强函数、类或其他可调用对象的行为,而无需更改其源代码。装饰器本质上是一个接收函数作为参数并返回新函数的函数,这使得它们非常适合用来...

    python-base.py: 千行代码入门Python python-visual.py: 15张图入门Matplotlib

    python_base.py: 千行代码入门Python python_visual.py: 15张图入门Matplotlib python_visual_animation.py: 使用Matplotlib画...python_decorator.py: Python进阶: 通过实例详解装饰器(附代码) python_datetime.p

    Python库 | cache_decorator-1.2.0.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:cache_decorator-1.2.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

Global site tag (gtag.js) - Google Analytics