【Python】 装饰器

装饰器

@wraps

def decorator(func):
    """this is decorator __doc__"""

    def wrapper(*args, **kwargs):
        """this is wrapper __doc__"""
        print("this is wrapper method")
        return func(*args, **kwargs)

    return wrapper


@decorator
def test():
    """this is test __doc__"""
    print("this is test method")


if __name__ == '__main__':
    print("__name__: ", test.__name__)
    print("__doc__:  ", test.__doc__)

"""
    __name__:  wrapper
    __doc__:   this is wrapper __doc__
"""
def decorator(func):
    """this is decorator __doc__"""

    @wraps(func)
    def wrapper(*args, **kwargs):
        """this is wrapper __doc__"""
        print("this is wrapper method")
        return func(*args, **kwargs)

    return wrapper


@decorator
def test():
    """this is test __doc__"""
    print("this is test method")


if __name__ == '__main__':
    print("__name__: ", test.__name__)
    print("__doc__:  ", test.__doc__)
    
"""
__name__:  test
__doc__:   this is test __doc__
"""

装饰函数

无参数

\[decorate(fun)(1, 2, c=3)\,==>\,inner(1, 2, c=3) \]

def decorate(fn):
    print("-decorate() start")

    @functools.wraps(fn)
    def inner(*args, **kwargs):
        print("--inner() start")
        print(f'--inner: args=={args}, kwargs=={kwargs}')
        fn(*args, **kwargs)
        print("--inner() end")

    print("-decorate() end")
    return inner


@decorate
def fun(a, b, c):
    print("----fun() start")
    print(f'----fun: a=={a}, b=={b}, c=={c}')
    print("----fun() end")


if __name__ == '__main__':
    print('-main() start')
    fun(1, 2, c=3)
    print('-main() end')

"""
-decorate() start
-decorate() end
-main() start
--inner() start
--inner: args==(1, 2), kwargs=={'c': 3}
----fun() start
----fun: a==1, b==2, c==3
----fun() end
--inner() end
-main() end
"""

有参数

\[decorate(-1,-2,-3)fun(1,2,c=3)\,==>\,wrap(fun)(1,2,c=3)\,==>\,inner(1,2,c=3) \]

def decorate(a, b, c):
    print('-decorate() start')

    def wrap(fn):
        print('-wrap() start')
        print(f'-warp: a=={a}, b=={b}, c=={c}')

        @functools.wraps(fn)
        def inner(*args, **kwargs):
            print("--inner() start")
            print(f'--inner: a=={a}, b=={b}, c=={c}')
            print(f'--inner: args=={args}, kwargs=={kwargs}')
            fn(*args, **kwargs)
            print("--inner() end")

        print('-wrap() end')
        return inner

    print('-decorate() end')
    return wrap


@decorate(-1, -2, -3)
def fun(a, b, c):
    print("----fun() start")
    print(f'----fun: a=={a}, b=={b}, c=={c}')
    print("----fun() end")


if __name__ == '__main__':
    print('-main() start')
    fun(1, 2, c=3)
    print('-main() end')

"""
-decorate() start
-decorate() end
-wrap() start
-warp: a==-1, b==-2, c==-3
-wrap() end
-main() start
--inner() start
--inner: a==-1, b==-2, c==-3
--inner: args==(1, 2), kwargs=={'c': 3}
----fun() start
----fun: a==1, b==2, c==3
----fun() end
--inner() end
-main() end
"""

装饰类

无参数

\[decorate(A)(1, 2, c=3)\,==>\,inner(1, 2, c=3) \]

def decorate(cls):
    print('-decorate() start')

    @functools.wraps(cls)
    def inner(*args, **kwargs):
        print("--inner() start")
        print(f'--inner: args=={args}, kwargs=={kwargs}')
        print("--inner() end")
        return cls(*args, **kwargs)

    print('-decorate() end')
    return inner


@decorate
class A:
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

        
if __name__ == '__main__':
    print('-main() start')
    a = A(1, 2, c=3)
    print('-main() end')
    
"""
-decorate() start
-decorate() end
-main() start
--inner() start
--inner: args==(1, 2), kwargs=={'c': 3}
--inner() end
-main() end
"""

有参数

\[decorate(-1, -2, -3)A(1, 2, c=3)\,==>\,wrap(A)(1, 2, c=3)\,==>\,inner(1, 2, c=3) \]

def decorate(a, b, c):
    print('-decorate() start')

    def wrap(cls):
        print('-wrap() start')
        print(f'-warp: a=={a}, b=={b}, c=={c}')

        @functools.wraps(cls)
        def inner(*args, **kwargs):
            print("--inner() start")
            print(f'--inner: a=={a}, b=={b}, c=={c}')
            print(f'--inner: args=={args}, kwargs=={kwargs}')
            print("--inner() end")
            return cls(*args, **kwargs)

        print('-wrap() end')
        return inner

    print('-decorate() end')
    return wrap


@decorate(-1, -2, -3)
class A:
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c


if __name__ == '__main__':
    print('-main() start')
    a = A(1, 2, c=3)
    print('-main() end')
    
"""
-decorate() start
-decorate() end
-wrap() start
-warp: a==-1, b==-2, c==-3
-wrap() end
-main() start
--inner() start
--inner: a==-1, b==-2, c==-3
--inner: args==(1, 2), kwargs=={'c': 3}
--inner() end
-main() end
"""

有无括号

\[decorate()fun(1, 2, c=3)\,==>\,inner(fun)(1, 2, c=3)\,==>\,fun(1, 2, c=3) \]

def decorate():
    print("-decorate() start")

    def inner(fn):
        print("--inner() start")
        print("--inner() end")
        return fn

    print("-decorate() end")
    return inner


@decorate()
def fun(a, b, c):
    print("----fun() start")
    print(f'----fun: a=={a}, b=={b}, c=={c}')
    print("----fun() end")


if __name__ == '__main__':
    print('-main() start')
    fun(1, 2, c=3)
    print('-main() end')
    
"""
-decorate() start
-decorate() end
--inner() start
--inner() end
-main() start
----fun() start
----fun: a==1, b==2, c==3
----fun() end
-main() end
"""
posted @ 2022-02-10 12:58  Vivid-BinGo  阅读(26)  评论(0编辑  收藏  举报