When writing processing code, in addition to the optional finally block, there is also an optional else block. Let me remind you that this block of code is executed if the try block is completed successfully, without flying exceptions.
Question: Why do I need an else block if everything can be placed in a try block?
This question has already been discussed in the English version of SO: Python try-else . But again, in my opinion, there is no clear and concrete answer, when it is really impossible to do without an else block and be better off with it.
So far, my understanding at the rule level (formulated by myself, regardless of anyone): If the else block was needed, then the function code is complicated. The code in the else should be placed in a separate method and called not in the piece of code where you want to write else , but in a higher level, i.e. higher level function.
finallyexecutes the block of instructions in any case, whether there was an exception or not (applicable, when you need to do something, for example, close the file). Theelseis executed if there was no exception (for example, in the case when you need to log the success of the action described in thetryblock). - StateItPrimitivetryblock and not to embed the logic of processing the success of performing the action directly intry(although, on the other hand, with the same success, this logic could be put into a separate function and embedded at the end oftry). But this is only an opinion from the side, because I am not a python programmer. - StateItPrimitiveelsecode looks clear - gil9redtryblock. And either the function solved the problem for which it was written, or not! And in the case of separatetryandelsecode is smeared and the attention of the βreaderβ is scattered - sys_dev