注册 登录
编程论坛 Python论坛

Python装饰器遇到的一个问题

bubble_soup 发布于 2018-04-13 08:05, 1498 次点击
def wrapper(func):
    def inner():
        print("I am the inner from the wrapper...")
    return inner

@wrapper
def caller():
    print("I am the caller...")

================

调用caller():
I am the inner from the wrapper...

===============

问题:caller原来的print语句呢?
1 回复
#2
lk1986ldlq2018-04-23 19:47
装饰后的caller指向了inner函数体,故调用caller():
I am the inner from the wrapper...
而wrapper(func)的参数func指向了caller函数体,故func():
I am the caller...
1