[Python-Dev] PEP 8 update

Guido van Rossum guido at python.org
Tue Apr 7 21:31:49 CEST 2015


Talk about hijacking an unrelated thread... :-(

Though there's maybe a style rule for the  ternary operator to be devised
here? :-)
On Apr 7, 2015 11:36 AM, "Stefan Behnel" <stefan_ml at behnel.de> wrote:

> Antoine Pitrou schrieb am 07.04.2015 um 14:26:
> > On Tue, 07 Apr 2015 03:11:30 +0100
> > Rob Cliffe <rob.cliffe at btinternet.com> wrote:
> >>
> >> 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 agree with you on this.
>
> +1, the ternary operator reads best when there is a "normal" case that can
> go first and a "special" or "unusual" case that can go last and does not
> apply under the (given) normal conditions.
>
> Stefan
>
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> https://mail.python.org/mailman/options/python-dev/guido%40python.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20150407/1343e690/attachment.html>


More information about the Python-Dev mailing list