我有一系列我所需要的呼吁,所有这些呼吁都可以成为一种例外,我希望能很好地保护这些呼吁。 我正设法找到更专业的方法,以在座:
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
是否有更好的办法这样做? 或许有说明?
为了澄清,Idont希望就原始方法说明:
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()