Closures and decorators use cases

'''
@Author: Feng Hao
@Date: 2019-12-04 22:58:49
@LastEditors: Feng Hao
@LastEditTime: 2019-12-05 00:03:25
@FilePath: \ bob_develop \ python \ closures and decorators .py
'''

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

@deco ( ' decorator mass participation ' )   # Without decorators parameter passing, func0 layer can be removed, and moved to the outermost func 
DEF say_hello (NUM):
     Print ( ' Hello ' , NUM)

say_hello ( ' function parameter passing ' )
 
operation result:

func0 parameter passing decorator
func1 function parameter passing
hello function parameter passing

 

Guess you like

Origin www.cnblogs.com/feng-hao/p/11986619.html