当前位置: 首页 > 后端技术 > Python

Python装饰器案例

时间:2023-03-26 17:48:01 Python

#-*-coding:utf-8-*-#author:baoshan#Functiondecoratorwithparametersdefsay_hello(country):defwrapper(func):defdeco(*args,**kwargs):ifcountry=='china':print('Hello!')elifcountry=='america':print('hello')else:returnfunc(*args,**kwargs)returndecoreturnwrapper@say_hello('中国')defchinese():print('我来自中国。')@say_hello('美国')defamerica():print('我来自美国。')america()print('-'*20)Chinese()输出结果:helloIamfromAmerica.--------------------你好!我来自中国。延伸阅读:https://www.cnblogs.com/serpe...https://www.cnblogs.com/liany...