Answer the question
In order to leave comments, you need to log in
Exiting a stream without a lot of checks?
Let's say I run the main function in the thread , it does something, while calling other sub_main_1,2,3... functions inside itself , and those others, and so on. And at some level main - sub_main_1 - sub_sub_... I catch an error, my own or, for example, an exception - no access. Therefore, I want to interrupt the execution of the thread. This means I have to return the result (some False flag) from the last function in the chain (or use a class variable, global, does not change the essence), adding checks everywhere after each function. How can this be avoided?
def main():
if not sub_main_1():
return
if not sub_main_2():
return
if not sub_main_3():
return
Answer the question
In order to leave comments, you need to log in
def sub_main_1():
return 1
def sub_main_2():
return 2
def sub_main_3():
1 / 0
funcs = [sub_main_1, sub_main_2, sub_main_3]
def main():
for func in funcs:
try:
if not func():
return
except Exception as e:
print(e)
return func, False
if __name__ == '__main__':
print(main())
division by zero
(<function sub_main_3 at 0x000002679A31B318>, False)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question