流畅的python--第七章--装饰器

装饰器基础

@decorate
def target():
    print('running target()')
    
def target():
    print('running target()')
target = decorate(target)
#以上两种相同

nonlocal声明

def make_average():
    count = 0
    total = 0
    
    def average(new_value):
        nonlocal count, total   #用于声明,否则会报错
        count += 1
        total += new_value
        return total/count
    
    return average
    ```
    
发布了69 篇原创文章 · 获赞 2 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42307828/article/details/104249640