
On Tue, Jun 15, 2021 at 09:48:48PM -0300, Soni L. wrote:
def foo(): try: return thing() except ValueError: try: return otherthing() except ValueError: try: return yetotherthing() except ValueError: if shouldraise(): raise
Look at all that unnecessary indentation! Would be nice to get rid of it.
It would be nice to use better looking four-space indents with indented blocks: def foo(): try: return thing() except ValueError: try: return otherthing() except ValueError: try: return yetotherthing() except ValueError: if shouldraise(): raise That makes the block structure of the code more clear, it reflects the semantics of the code better, it is much easier to modify (if we add an extra line between one of the try keywords and the returns, we don't need to change any existing lines, we just insert a new one: smaller diffs and better diffs). And it makes it more obvious that there is no final return (perhaps we forgot to add it?) and that the interpreter will automatically return None when we fall off the end of the function. But if none of those arguments convince you that ideomatic Python code is better Python code, that's okay too. You can remove almost all the indentation: def foo(): try: return thing() except ValueError: pass try: return otherthing() except ValueError: pass try: return yetotherthing() except ValueError: if shouldraise(): raise -- Steve