MST

星途 面试题库

面试题:Python装饰器嵌套使用基础

请编写一个Python程序,包含两个装饰器`decorator1`和`decorator2`,并展示它们嵌套使用的效果。要求`decorator1`在函数执行前打印'Before function by decorator1',`decorator2`在函数执行后打印'After function by decorator2'。
25.9万 热度难度
编程语言Python

知识考点

AI 面试

面试题答案

一键面试
def decorator1(func):
    def wrapper():
        print('Before function by decorator1')
        func()
    return wrapper


def decorator2(func):
    def wrapper():
        func()
        print('After function by decorator2')
    return wrapper


@decorator1
@decorator2
def my_function():
    print('Inside my_function')


my_function()