python 装饰器案例
文章目录
# -*- coding: utf-8 -*-
# author:baoshan
# 带参数的函数装饰器
def say_hello(country):
def wrapper(func):
def deco(*args, **kwargs):
if country == 'china':
print('你好!')
elif country == 'america':
print('hello')
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()
输出结果:
hello
I am from America.
--------------------
你好!
我来自中国。扩展阅读: https://www.cnblogs.com/serpe...
https://www.cnblogs.com/liany...
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论