zoukankan      html  css  js  c++  java
  • 装饰器的简单使用

    首先,装饰器是什么?

    装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象。它经常用于有切面需求的场景,比如:插入日志、性能测试、事务处理、缓存、权限校验等场景。装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量与函数功能本身无关的雷同代码并继续重用。概括的讲,装饰器的作用就是为已经存在的对象添加额外的功能。(摘自知乎 @zhijun-liu)

    下面介绍装饰器的用法.

    参考了http://www.cnblogs.com/rhcad/archive/2011/12/21/2295507.html ,并且有一些小改动

    一简单装饰函数

    def demo(func):

        print("**********")

    func()

    print("##########")

        return func

    二 使用@

    为了结果更加明显,我新加了一个函数

    def a():

        print("hhhh")

    def demo(func):

        print("**********")

        func()

        a()

        print("##########")

        return a

    #

    @demo

    def myfun():

        print("myfunc() called.")

        return 0

     

    myfun()

    myfun()

     

    **********

    myfunc() called.

    hhhh

    ##########

    hhhh

    hhhh

    myfun

    **********

    myfunc() called.

    hhhh

    ##########

    myfun=demo(myfun)

    实际上相当于执行了左侧的命令,

    print(myfun.__name__)得到最终的赋值结果:a   <class 'function'>

     

    三. 内嵌包装函数(闭包)

    def deco(func):

        def _deco(a,b):

            print("y")

            print("******************.")

            re=func(a,b)

            print("######################")

            # 不需要返回func,实际上应返回原函数的返回值

            return re

        print("pass")

        return _deco

    @deco

    def myfunc(a,b):

        print(a+b)

        print(" myfunc() called.")

        return 'ok'

    myfunc("hello","world")

    myfunc("麻蛋","啊")

    print(myfunc("hello","world"))

    pass

    y

    ******************.

    helloworld

     myfunc() called.

    ######################

    y

    ******************.

    麻蛋啊

     myfunc() called.

    ######################

    y

    ******************.

    helloworld

     myfunc() called.

    ######################

    ok

    Process finished with exit code 0

    四.使用可变参数

    def deco(func):

        def _deco(*args,**kwargs):

            print("y")

            print("******************.")

            re=func(*args,**kwargs)

            print("######################")

            return re

        print("pass")

        return _deco

    补充:可变参数:

    http://blog.csdn.net/chenjinyu_tang/article/details/8136841

    参考上面的网址

    一句话解释:

    *args :普通参数,可以认为是一个list

    **kwargs 可以认为是一个字典,需要输入key和value

    五装饰器带参数

    def zhuangshiqi(a):

        def deco(func):

            def _deco(*args,**kwargs):

                print("y")

                print("******************.")

                print(a)

                re=func(*args,**kwargs)

                print("######################")

                return re

            print("pass")

            return _deco

        return deco

    @zhuangshiqi("test")

    def myfunc(a,b):

        print(a+b)

        print(" myfunc() called.")

    return 'ok'

    myfunc("hello","world")  

    myfunc("麻蛋","啊")

    # 创建装饰函数,传入的参数为cls

    def hello(cls):

        # 传入装饰的函数名

        def _hello(func):

            #可以传入func函数的参数

            def __hello(*args,**kwargs):

                #下面开始编写方法

                #这里因为不知道cls为哪个类,所以会出现IDE无法自动提示的情况

                cls.acquire()

                # 得到func的返回值

                re=func()

                cls.release()

                # 返回re

                return re

            return __hello

    return _hello

    注意:类中 的方法需要使用@classmethod或者@staticmethod标记

  • 相关阅读:
    Java两种方法实现循环报数
    不安装 oracle的客户,就可以使用pl/sql访问远程oracle 数据库的方法
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    从Oracle到PostgreSQL:最全控制文件
    springboot访问静态资源
    计算机组成原理 — 指令系统
  • 原文地址:https://www.cnblogs.com/twotigers/p/6913613.html
Copyright © 2011-2022 走看看