A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2015-April/139061.html below:

[Python-Dev] PEP 8 update

[Python-Dev] PEP 8 update [Python-Dev] PEP 8 updateBen Hoyt benhoyt at gmail.com
Tue Apr 7 14:47:25 CEST 2015
> My own preference would be:
>
>     def foo(x):
>         if x >= 0:
>             return math.sqrt(x)
>         return None

Kind of getting into the weeds here, but I would always invert this to
"return errors early, and keep the normal flow at the main indentation
level". Depends a little on what foo() means, but it seems to me the
"return None" case is the exceptional/error case, so this would be:

def foo(x):
    if x < 0:
        return None
    return math.sqrt(x)

The handling of errors is done first, and the normal case stays at the
main indentation level. Is this discussed in style guides at all? I
don't see anything directly in PEP 8, but I might be missing
something.

Oh wait, I just noticed this is exactly how Guido has it in his PEP
addition with the definition of bar(). :-|

-Ben
More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4