Python学习打卡--day29(基础学习:闭包例子)

小例子一
# 闭包小栗子
def who(name):
    def do(what):
        print("{} say:{}".format(name, what))

    return do


lucy = who('luch')
lily = who('lily')

lucy('i want to eat fish')
lily('it is ok')

打印项目日志

# 闭包实现快速给不同项目记录日志
import logging


def log_header(logger_name):
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(name)s] %(levelname)s  %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    logger = logging.getLogger(logger_name)

    def _logging(someting, level):
        if level == 'debug':
            logger.debug(someting)
        elif level == 'warning':
            logger.warning(someting)
        elif level == 'error':
            logger.error(someting)
        else:
            raise Exception("I dont know what you want to do?")

    return _logging


project_1_logging = log_header('project_1')
project_2_logging = log_header('project_2')


def project_1():
    # do something
    project_1_logging('this is a debug info', 'debug')
    project_1_logging('this is a warning info', 'warning')
    project_1_logging('this is a error info', 'error')


def project_2():
    # do something
    project_2_logging('this is a debug info', 'debug')
    project_2_logging('this is a warning info', 'warning')
    project_2_logging('this is a error info', 'error')


project_1()
project_2()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值