# 闭包小栗子defwho(name):defdo(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
deflog_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')defproject_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')defproject_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()