闭包和装饰器使用案例

'''
@Author: 冯浩
@Date: 2019-12-04 22:58:49
@LastEditors: 冯浩
@LastEditTime: 2019-12-05 00:03:25
@FilePath: \bob_develop\python\闭包和装饰器.py
'''

def deco(str_):
    def func0(func):
        print('func0', str_)
        def func1(num):
            print('func1', num)
            return func(num)
        return func1
    return func0

@deco('装饰器传参')  # 若无装饰器传参,则可以去掉func0层,并将func移至最外层
def say_hello(num):
    print('hello', num)

say_hello('函数传参')
 
运行结果:

func0 装饰器传参
func1 函数传参
hello 函数传参

 

猜你喜欢

转载自www.cnblogs.com/feng-hao/p/11986619.html