标题:Python装饰器遇到的一个问题
只看楼主
bubble_soup
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2018-1-21
结帖率:0
 问题点数:0 回复次数:1 
Python装饰器遇到的一个问题
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语句呢?
搜索更多相关主题的帖子: Python 装饰器 inner print the 
2018-04-13 08:05
lk1986ldlq
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2018-4-23
得分:0 
装饰后的caller指向了inner函数体,故调用caller():
I am the inner from the wrapper...
而wrapper(func)的参数func指向了caller函数体,故func():
I am the caller...
2018-04-23 19:47



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-486028-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 2.228094 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved