闭包和装饰器使用案例

文山羊 2019-12-05

‘‘‘
@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 函数传参

相关推荐

zhuxianfeng / 0评论 2020-02-09