나는 내가해야 할 일련의 호출을 가지고 있는데,이 모든 것은 예외를 던질 수 있고 호출을 보호하기위한 좋은 방법이 필요하다. 파이썬에서 다음을 수행하는보다 전문적인 방법을 찾으려고합니다.파이썬에서 보호 된 메서드 호출?
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()
예외를 무시 하시겠습니까? 그것은 좋은 생각처럼 보이지 않습니다. [with] (http://docs.python.org/reference/compound_stmts.html#the-with-with-statement) 및 [finally] (http://docs.python.org/reference/compound_stmts.html#the -try-statement). – nmichaels
나에게 잘 어울리지 만, 왜 모든 예외를 무시하는 테스트가 필요하겠습니까? –
현실에서는 모든 통신/전송 예외를 잡으려고합니다. 그러나 exapmle에 대해서는 복잡한 것을 원하지 않았습니다. 이것은 종료 프로세스의 일부이므로 gracefuly를 종료 할 스레드를 요청하고 있습니다 ... 계속 진행할 때주의 할 사항이 없다면. 나는 예외 목록을 protected_call ..에 전달하는 것에 대해 생각했다. – Nix