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
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/O5VGFNADFNTQ5CDGZTUK347JBPD3FGWR/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to