首页 > 编程语言 > 详细

Python带参数的函数装饰器

时间:2019-08-19 12:21:13      阅读:156      评论:0      收藏:0      [点我收藏+]

 

 

# -*- 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://zhuanlan.zhihu.com/p/65968462

 

Python带参数的函数装饰器

原文:https://www.cnblogs.com/zhzhang/p/11375574.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!