Код: Выделить всё
def function():
"""
A function that do something, and return True if no exception occurred, False otherwise.
"""
try:
# Do whatever it should
return True
except:
return False
Код: Выделить всё
def false_if_exception(func):
"""
Make the function return True if it goes without exceptions
"""
def wrapper(*args, **kwargs):
try:
func(*args, **kwargs)
except Exception as e:
print(f"DEBUG: an exception thrown while calling {func.__name__}: {e}")
return False
return True
return wrapper
# Then it can be used like this
@false_if_exception
def function():
....
Это так? питонический, чтобы сделать это?
Подробнее здесь: https://stackoverflow.com/questions/793 ... value-type
Мобильная версия