79-定义不同颜色字体

使用装饰器定义调用不通颜色的字体:

def color(c):
    def set_color(func):
        def red(*word):
            return '\033[31;1m%s\033[0m' % func(*word)
        def green(*word):
            return '\033[32;1m%s\033[0m' % func(*word)
        adict = {'red':red,"green":green}
        return adict[c]
    return set_color

@color('red')
def boy():
    return "Hello boy!!!"

@color('green')
def girl(name):
    return "Hello %s!!!" %name

if __name__ == "__main__":
    print(boy())
    print(girl('xiaohong'))

结果输出:

 

posted @ 2019-06-18 10:29  hejp  阅读(173)  评论(0编辑  收藏  举报