style: single and multiple lines

Steve D'Aprano steve+python at pearwood.info
Mon Oct 2 12:56:50 EDT 2017


On Tue, 3 Oct 2017 03:00 am, Stefan Ram wrote:

>   My copy of pep 8 (from 2016) says:

Why don't you look at the current version, which is conveniently available to
anyone on the internet, for free?

https://www.python.org/dev/peps/pep-0008/‎


(Last commit to the PEP was Jul 12, 2017.)


 
> Yes:
> 
> def f(x): return 2*x
> 
>   . So this single-line style should not be that bad.

That is specifically contrasted with using a lambda:

f = lambda x: 2*x

It shouldn't be read as a general approval to try to fit functions all in one
line.



>   So, is this better:
> 
> def f(x):
>     return 2*x

Yes.


>   ? And is
> 
> def f(x):
>     y = x*2
>     return y
> 
>   better than
> 
> def f(x):
>     y = x*2; return y

Hell yes! A thousand times YES.


One statement per line, except under the most unusual circumstances. Semi-colon
separated statements are a convenience of use at the command line and REPL.



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list