Skip to main content
 首页 » 编程设计

python之装饰器链接如何工作

2025年05月04日11haluo1

<分区>

我在玩装饰器,想出了以下代码。

def first(fn): 
    def wrapper(): 
        print 'Before First' 
        fn() 
    return wrapper 
 
def second(fn): 
    def wrapper(): 
        print 'Before Second' 
        fn() 
    return wrapper 
 
@second 
@first 
def fn(): 
    print 'The Original Function' 
 
fn() 

我得到如下输出

Before Second 
Before First 
The Original Function 

我真的没有得到订单。我在某处读到装饰器以相反的顺序被调用,我的意思是第一个应该被称为第一个,第二个应该被称为第二个。但输出表明并非如此。 这里发生了什么??