python 使用此包装器而不是崩溃从函数中获取默认输出。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用此包装器而不是崩溃从函数中获取默认输出。相关的知识,希望对你有一定的参考价值。
from functools import wraps
class default_output(object):
"""returns the backup plan if the decorated method fails
by: Cody Kochmann
"""
def __init__(self, backup_output, logger=print):
self.backup_output = backup_output
self.logger = logger
def __call__(self,func):
assert callable(func), "default_output needs a callable target to wrap"
def wrapper(*args, **kwargs):
print(args,kwargs)
try:
return func(*args, **kwargs)
except Exception as ex:
# this is where logging would happen
self.logger(ex)
# ====================================
return self.backup_output
return wrapper
以上是关于python 使用此包装器而不是崩溃从函数中获取默认输出。的主要内容,如果未能解决你的问题,请参考以下文章