Loading

Python装饰器总结

装饰器

# --------------- 装饰器 --------------- #
def decorator1(func):  # 传入被装饰的函数
    # 嵌套内函数可以看做被装饰的函数
    def inner():
        print("-------- inner --------")
        func()
        print("-----------------------")

    return inner


@decorator1
def func1():
    print("This is func1...")


func1()
print()

打印结果:

-------- inner --------
This is func1...
-----------------------

被装饰的函数带参数的装饰器

# --------------- 被装饰的函数带参数的装饰器 --------------- #
def decorator2(func):  # 传入被装饰的函数
    # 嵌套内函数可以看做被装饰的函数,所以被装饰函数的参数要写在inner的形参列表里,调用被装饰的函数时传入
    def inner(arg):
        print("-------- inner --------")
        func(arg)
        print("-----------------------")

    return inner


@decorator2
def func2(arg):
    print(f"This is func2...arg:{arg}")


func2("hello")
print()

打印结果:

-------- inner --------
This is func2...arg:hello
-----------------------

带返回值的函数的装饰器

# --------------- 带返回值函数的装饰器 --------------- #
def decorator3(func):  # 传入被装饰的函数
    # 嵌套内函数可以看做被装饰的函数,所以被装饰函数的参数要写在inner的形参列表里,调用被装饰的函数时传入
    def inner(arg):
        print("-------- inner --------")
        str_ = func(arg)
        print("-----------------------")
        return str_

    return inner


@decorator3
def func3(arg):
    return f"This is func3...arg:{arg}"


print(func3("hello"), "\n")

打印结果:

-------- inner --------
-----------------------
This is func3...arg:hello 

带参数的装饰器

# --------------- 带参数的装饰器 --------------- #
def decorator4(deco_arg):  # 带参数的装饰器再嵌套一层函数,传入装饰器的参数
    def outer(func):  # 传入被装饰的函数
        # 嵌套内函数可以看做被装饰的函数,所以被装饰函数的参数要写在inner的形参列表里,调用被装饰的函数时传入
        def inner(arg):  # 被装饰的函数的参数
            print("-------- inner --------")
            print("decorator_arg:", deco_arg)
            func(arg)
            print("-----------------------")

        return inner

    return outer


# 括号表示函数的调用,可以理解为此时调用了decorator4函数,此函数返回一个真正要用的装饰器outer
@decorator4("deco_arg")
def func4(arg):
    print(f"This is func4...arg:{arg}")


func4("hello")
print()

打印结果:

-------- inner --------
decorator_arg: deco_arg
This is func4...arg:hello
-----------------------

多个装饰器

# --------------- 多个装饰器 --------------- #
def decorator5(func):
    print("This is decorator5...")

    def inner(arg):  # 被装饰的函数inner及其参数
        print("----- decorator5 inner -----")
        func(arg)  # 此时调用的是inner(arg)
        print("----------------------------")

    return inner


def decorator6(func):
    print("This is decorator6...")

    def inner(arg):  # 被装饰的函数func4及其参数
        print("----- decorator6 inner -----")
        func(arg)  # 调用func5
        print("----------------------------")

    return inner


@decorator5
@decorator6
def func5(arg):
    print(f"This is func5... arg:{arg}")


func5("hello")

# 等同于
# func4 = decorator4(decorator5(func4))
# func4("hello")

打印结果:

This is decorator6...
This is decorator5...
----- decorator5 inner -----
----- decorator6 inner -----
This is func5... arg:hello
----------------------------
----------------------------
posted @ 2021-05-02 14:47  七年i  阅读(60)  评论(0)    收藏  举报