Python中受保护的方法调用?
我需要进行一系列调用,所有调用都可能引发异常,我需要一种保护调用的好方法。我正试图找到一种更专业的方法来使用python执行以下操作:Python中受保护的方法调用?,python,exception-handling,annotations,Python,Exception Handling,Annotations,我需要进行一系列调用,所有调用都可能引发异常,我需要一种保护调用的好方法。我正试图找到一种更专业的方法来使用python执行以下操作: def protected_call(method): result = None try: result= method() except: pass return result class Test(): def terminate(): protected_call(self.could_thr
def protected_call(method):
result = None
try:
result= method()
except: pass
return result
class Test():
def terminate():
protected_call(self.could_throw_exception)
protected_call(self.receiver.stop)
protected_call(self.connection.stop)
#etc
有更好的方法吗?也许有注释
为了澄清,我不想在原始方法上添加注释,即:
class Receiver():
@protected
def stop():
print 'I dont want to do this'
class Test():
@protected
def could_throw_exception():
print 'dont want this'
def stop():
self.could_throw_exception()
self.receiver.stop()
这就是我想要的:
class Receiver():
def stop():
print 'I want this'
class Test():
def could_throw_exception():
print 'like this'
'''This one cares about crashing'''
def stop()
self.could_throw_exception()
self.receiver.stop()
self.connection.stop()
'''This one does not'''
def terminate():
#i want to define it at the call level.
@protected
self.could_throw_exception()
@protected
self.receiver.stop()
Decorator非常适合这样做:
def protected_call(method):
def wrapper(*args, **kwargs):
try:
return method(*args, **kwargs)
except:
pass
return wrapper
示例用法:
@protected_call
def foo():
raise Exception()
# nothing is being raised
foo()
Decorator非常适合这样做:
def protected_call(method):
def wrapper(*args, **kwargs):
try:
return method(*args, **kwargs)
except:
pass
return wrapper
示例用法:
@protected_call
def foo():
raise Exception()
# nothing is being raised
foo()
至少日志记录异常在这里似乎是一个有用的功能,或者您如何控制意外错误…?至少日志记录异常在这里似乎是一个有用的功能,或者您如何控制意外错误…?听起来像是装饰师的工作
def protected_call(func):
def inner(*args, **kw):
try:
return func(*args, **kw)
except:
pass
return inner
class Test():
@protected_call
def throws_exception(self):
print 1/0
@protected_call
def no_exception(self):
print 4
def sometimes_need_exception(self):
print 5
protected_sometimes_need_exception = protected_call(sometimes_need_exception)
def stop(self):
self.throws_exception()
self.no_exception()
听起来像是装修工的工作
def protected_call(func):
def inner(*args, **kw):
try:
return func(*args, **kw)
except:
pass
return inner
class Test():
@protected_call
def throws_exception(self):
print 1/0
@protected_call
def no_exception(self):
print 4
def sometimes_need_exception(self):
print 5
protected_sometimes_need_exception = protected_call(sometimes_need_exception)
def stop(self):
self.throws_exception()
self.no_exception()
正如nmichaels所建议的,最好通过
with
语句来处理这种事情
@contextlib.contextmanager
def suppress_exceptions(*exceptions):
if not exceptions:
exceptions = Exception
try:
yield
except exceptions:
# You would log an error here
# If you have logging in your application
pass
with suppress_exceptions():
1/0
print("Ignored the exception!")
with suppress_exceptions(IOError):
1/0
# The second one will let the exception through
正如nmichaels所建议的,最好通过
with
语句来处理这种事情
@contextlib.contextmanager
def suppress_exceptions(*exceptions):
if not exceptions:
exceptions = Exception
try:
yield
except exceptions:
# You would log an error here
# If you have logging in your application
pass
with suppress_exceptions():
1/0
print("Ignored the exception!")
with suppress_exceptions(IOError):
1/0
# The second one will let the exception through
是否要忽略异常?这似乎不是个好主意。查看和。对我来说很好,但是,为什么您希望一个测试忽略所有异常?实际上,我将捕获所有通信/传输异常,但举个例子,我不想让事情复杂化。这是关机过程的一部分,所以我要求一个线程优雅地关机。。。如果没有我不在乎bc,我将继续前行。我考虑过向protected_调用传递一个异常列表。旁注:通过删除
result
变量,可以简化protected_调用()
函数,如下所示:try:return方法();除了:pass
(不需要其他内容,因为函数默认返回None)。是否忽略异常?这似乎不是个好主意。查看和。对我来说很好,但是,为什么您希望一个测试忽略所有异常?实际上,我将捕获所有通信/传输异常,但举个例子,我不想让事情复杂化。这是关机过程的一部分,所以我要求一个线程优雅地关机。。。如果没有我不在乎bc,我将继续前行。我考虑过向protected_调用传递一个异常列表。旁注:通过删除result
变量,可以简化protected_调用()
函数,如下所示:try:return方法();除了:pass
(不需要其他任何东西,因为函数默认不返回任何值)。我拥有一个线程,我要求关闭它,如果它做得不优雅,我真的不在乎(在某种程度上,我关心mem泄漏),bc我将重新初始化所有内容。我拥有一个线程,我要求关闭它,如果它做得不优雅,我真的不在乎(在某种程度上,我在乎mem泄漏),bc我将重新初始化一切。我希望在我调用它时能够做到这一点,因为在某些情况下,人们需要知道这引发了异常。我猜我得在另一个电话里把这个包起来。如上所述,我添加了一个附加部分。@Nix,我添加了一个示例,其中修饰的方法绑定到不同的名称。我是否无法包装所有调用?是的,Python中没有关于错误恢复下一步的。我希望在调用它时能够做到这一点,因为在某些情况下,人们需要知道这引发了异常。我猜我得在另一个电话里把这个包起来。如上所述,我添加了一个附加部分。@Nix,我添加了一个示例,其中修饰的方法绑定到不同的名称。我是否无法包装所有调用?是的,Python中没有关于错误恢复下一步的。
。修饰程序的工作方式就好像他编写了foo=protected\u call(foo)一样。我希望能够在实例上定义它。。。在某些情况下,其他客户机希望调用foo()并看到异常。我将尝试在上面解释……那么你的方法可能更好。使用decorator,您必须执行以下操作:protected\u foo=protected\u call(foo);受保护的_foo()
因此这实际上是更多的代码。或者,您可以使用元类自动添加每个方法的受保护版本。这可以通过为给定类中的每个方法添加protected\u XXX
方法来实现。如果您认为元类是可行的,我可以向您展示一个元类示例。我希望以某种方式在实例级别利用注释。。如果这不可能,我可能会留下我所拥有的。装饰者的工作方式就像他编写了foo=protected\u call(foo)一样,我希望能够在实例上定义它。。。在某些情况下,其他客户机希望调用foo()并看到异常。我将尝试在上面解释……那么你的方法可能更好。使用decorator,您必须执行以下操作:protected\u foo=protected\u call(foo);受保护的_foo()
因此这实际上是更多的代码。或者,您可以使用元类自动添加每个方法的受保护版本。这可以通过为给定类中的每个方法添加protected\u XXX
方法来实现。如果您认为元类是可行的,我可以向您展示一个元类示例。我希望以某种方式在实例级别利用注释。。如果不可能,我可能会留下我所拥有的。这会很混乱,但我认为这是实现我所需要的唯一方法(实例级“装饰者”)这会很混乱,但我认为这是实现我所需要的唯一方法(实例级“装饰者”)