[Python-Dev] PEP 8 update (original) (raw)

Ben 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