Python function with parameters decorator

 

 

# - * - Coding: UTF-. 8 - * - 
# author: Baoshan 
# Functions decorator with parameters 


DEF say_hello (Country):
     DEF warpper (FUNC):
         DEF Deco (* args, ** kwargs):
             IF Country == ' china ' :
                 Print ( ' ! hello ' )
             elif Country == ' america ' :
                 Print ( ' the hello ' )
             the else :
                 return 
            FUNC (*args, **kwargs)
        return deco
    return wrapper


@say_hello('china')
def chinese():
    print('我来自中国。')


@say_hello('america')
def america():
    print('I am from America.')


america()
print('-'*20)
chinese()

 

Output:

the Hello 
the I AM from America.
 -------------------- 
Hello! 
I come from China.

 

Function with parameters decorator

Reference from: https://zhuanlan.zhihu.com/p/65968462

 

Guess you like

Origin www.cnblogs.com/zhzhang/p/11375574.html