[Python-Dev] PEP 8 update

Rob Cliffe rob.cliffe at btinternet.com
Tue Apr 7 04:11:30 CEST 2015



On 07/04/2015 02:08, Guido van Rossum wrote:
> I've taken the liberty of adding the following old but good rule to 
> PEP 8 (I was surprised to find it wasn't already there since I've 
> lived by this for ages):
>
>  *
>
>     Be consistent in return statements. Either all return statements
>     in a function should return an expression, or none of them should.
>     If any return statement returns an expression, any return
>     statements where no value is returned should explicitly state this
>     asreturn None, and an explicit return statement should be present
>     at the end of the function (if reachable).
>
>     Yes:
>
>     def foo(x):
>          if x >= 0:
>              return math.sqrt(x)
>          else:
>              return None
>
That would seem to be good style and common sense.

As a matter of interest, how far away from mainstream am I in 
preferring, *in this particular example* (obviously it might be 
different for more complicated computation),

     def foo(x):
         return math.sqrt(x) if x >= 0 else None

I probably have a personal bias towards compact code, but it does seem 
to me that the latter says exactly what it means, no more and no less, 
and therefore is somewhat more readable.  (Easier to keep the reader's 
attention for 32 non-whitespace characters than 40.)

Sorry if this is irrelevant to Guido's point.
Rob Cliffe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20150407/f389b749/attachment.html>


More information about the Python-Dev mailing list