Repository navigation
Conversation
|
ntBre
left a comment
There was a problem hiding this comment.
Thanks, this looks like an improvement, but I had an idea for making it slightly clearer, based on Micha's earlier suggestion.
First, I think this is complicated enough to justify a little extra explanation. Instead of the simple "Use instead:" I think we probably want to write something more like "Instead, move the return out of the finally clause and reserve the finally for any cleanup that must run regardless of other returns."
I also think an additional except arm like in Micha's suggestion would help, something like this for the "Instead..." example:
def process_data(data):
try:
return transform(data)
except ValueError:
return None
except TypeError:
log_error(data)
finally:
cleanup(data)
return -1but then we'd also need to update the initial example, so maybe just expanding the Instead... is fine with the example you have now. I just liked retaining the return -1 somewhere, but it requires a non-returning except arm to have any effect.
Closes #22325. Replaces the broken
SIM107documentation example that overwrites a return variable with one that correctly demonstrates a cleanup action in thefinallyblock.