semi colonic

avi.e.gross at gmail.com avi.e.gross at gmail.com
Thu Feb 23 15:26:53 EST 2023


Rob,

It depends. Some purists say python abhors one liners. Well, I politely disagree and I enjoyed this book which shows how to write some quite compressed one-liners or nearly so.

Python One-Liners: Write Concise, Eloquent Python Like a Professional Illustrated Edition
by Christian Mayer (Author)

https://www.amazon.com/Python-One-Liners-Concise-Eloquent-Professional/dp/1718500505/ref=sr_1_1?crid=2MMIRHGLR3GHN&keywords=python+one+liners&qid=1677183160&sprefix=python+one+liner%2Caps%2C93&sr=8-1

The reality is that python is chock full of constructs that make one-liners easy and perhaps make a need for semi-colons less crucial.

An example is a comprehension like:

[x*y for x in range(10) for y in range(10) if x != y ]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 4, 5, 6, 7, 8, 9, 0, 2, 6, 8, 10, 12, 14, 16, 18, 0, 3, 6, 12, 15, 18, 21, 24, 27, 0, 4, 8, 12, 20, 24, 28, 32, 36, 0, 5, 10, 15, 20, 30, 35, 40, 45, 0, 6, 12, 18, 24, 30, 42, 48, 54, 0, 7, 14, 21, 28, 35, 42, 56, 63, 0, 8, 16, 24, 32, 40, 48, 56, 72, 0, 9, 18, 27, 36, 45, 54, 63, 72]

How many lines of code would it take to make than nonsense using an initializer for an empty list and nested loops and an "if" statement?

A barely longer one-liner add more functionality with no added lines or semicolons:

[(x*y, x+y, x>=y) for x in range(10) for y in range(10) if x != y  ]
[(0, 1, False), (0, 2, False), ..., (72, 17, True)]

Examples of all kinds of such things about including seemingly trivial things like how a "with" statement lets you hide lots of code to do when entering and exiting use of an object. I have earlier mentioned the way packing and unpacking can effectively replace many lines of code with one.

So the pythonic way often is not so much to do things one many lines but often to do things in a way that a unit of logic often can fit on one screen by using what the language offers judiciously even if you do not put multiple statement with semicolons on one line.

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com at python.org> On Behalf Of Rob Cliffe via Python-list
Sent: Thursday, February 23, 2023 6:08 AM
To: python-list at python.org
Subject: Re: semi colonic



On 23/02/2023 02:25, Hen Hanna wrote:
>
> i sometimes  put  extra  commas...  as:
>
>                                 [  1, 2,  3,  4,     ]
That is a good idea.
Even more so when the items are on separate lines:
     [
         "spam",
         "eggs",
         "cheese",
     ]
and you may want to change the order.
>
> so it is (or may be)  easier  to add things   later.
>
>             -----------  i can think of putting extra final  ;   for the same reason.
That may not be such a good idea.  Writing multiple statements on one line is generally discouraged (notwithstanding that IMO it is occasionally appropriate).

Rob Cliffe
--
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list