您的位置:首页 > 编程语言 > Python开发

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

2019-06-01 22:28 627 查看

小例子一

# 闭包小栗子
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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: