Mailing List Archive

semi colonic
Thomas,

This is one of many little twists I see between languages where one feature
impacts use or even the need for another feature.

So can anyone point to places in Python where a semicolon is part of a best
or even good way to do anything?

Some older languages had simple parsers/compilers that needed some way to
know when a conceptual line of code was DONE and the semi-colon was a choice
for making that clear. But some languages seem to only continue looking past
an end-of-line if they detect some serious reason to assume you are in
middle of something. An unmatched open parenthesis or square bracket might
be enough, and in some languages a curly brace.

Python mainly has a concept of indentation and blank lines as one part of
the guidance. Continuing lines is possible, if done carefully.

But consider the lowly comma. Some languages may assume more is to come if
it is dangled at the end of a line. But in a language that supports a
dangling comma such as in making a tuple, how is the interpreter to know
more is to come?

>>> a = 5,
>>> a
(5,)

>>> a = 5, \
... 6
>>> a
(5, 6)

Well, one possible use of a semi-colon is to make short one-liner functions
like this:

def twoByFour(a): sq = a*a; forth = sq*sq; return((sq, forth))

There is no reason, of course, that could not be done in multiple indented
lines or other ways.

So if it was allowed in something like a lambda creation, it could be useful
but it isn't!

About the only thing that I can think of is if someone wishes to compress a
file of python code a bit. The indentation can add up but a semi-colon does
not solve all such problems.

Would anything serious break if it was deprecated for use as a statement
terminator? Then again, is it hurting anything? If it stopped being used
this way, could it later be introduced as some new language feature or
operator such as we now have a := b as a reuse of the colon, maybe a
semicolon could be useful at least until someone decides to allow additional
Unicode characters!

Now if there are serious reasons to use semi-colon in python, great. If not,
it is a historical artifact.

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of Thomas Passin
Sent: Wednesday, February 22, 2023 7:24 PM
To: python-list@python.org
Subject: Re: Introspecting the variable bound to a function argument

On 2/22/2023 3:12 PM, Hen Hanna wrote:
> On Wednesday, February 22, 2023 at 2:32:57 AM UTC-8, Anton Shepelev wrote:
>> Hello, all.
>>
>> Does Python have an instrospection facility that can determine to
>> which outer variable a function argument is bound, e.g.:
>>
>> v1 = 5;
>> v2 = 5;
>
>
> do some Python coders like to end lines with ; ?

Very few, probably. It's not harmful but adds unnecessary visual clutter.

>>
>> def f(a):
>> print(black_magic(a)) # or
black_magic('a')
>>
>> f(v1) # prints: v1
>> f(v2) # prints: v2
>>
>
> the term [call by name] suggests this should be possible.
>
>
> 30 years ago... i used to think about this type of thing A LOT ---
> ------- CBR, CBV, CBN, (call by value), (call by name)....
etc.
>

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

--
https://mail.python.org/mailman/listinfo/python-list
Re: semi colonic [ In reply to ]
On 2/22/2023 7:58 PM, avi.e.gross@gmail.com wrote:
> Thomas,
>
> This is one of many little twists I see between languages where one feature
> impacts use or even the need for another feature.
>
> So can anyone point to places in Python where a semicolon is part of a best
> or even good way to do anything?

Mostly I use it to run small commands on the command line with python
-c. e.g.

python -c "import sys;print('\n'.join(sys.path))"

This is handy enough that I wouldn't like to do without.

Another place I use the semicolon (once in a while) is for quick
debugging. I might add as line like, perhaps,

import os; print(os.path.exists(filename))

This way I can get rid of the debugging statement by deleting that
single line. This is non only quicker but I'm less likely to delete too
much by mistake.

> Some older languages had simple parsers/compilers that needed some way to
> know when a conceptual line of code was DONE and the semi-colon was a choice
> for making that clear. But some languages seem to only continue looking past
> an end-of-line if they detect some serious reason to assume you are in
> middle of something. An unmatched open parenthesis or square bracket might
> be enough, and in some languages a curly brace.
>
> Python mainly has a concept of indentation and blank lines as one part of
> the guidance. Continuing lines is possible, if done carefully.
>
> But consider the lowly comma. Some languages may assume more is to come if
> it is dangled at the end of a line. But in a language that supports a
> dangling comma such as in making a tuple, how is the interpreter to know
> more is to come?
>
>>>> a = 5,
>>>> a
> (5,)
>
>>>> a = 5, \
> ... 6
>>>> a
> (5, 6)
>
> Well, one possible use of a semi-colon is to make short one-liner functions
> like this:
>
> def twoByFour(a): sq = a*a; forth = sq*sq; return((sq, forth))
>
> There is no reason, of course, that could not be done in multiple indented
> lines or other ways.
>
> So if it was allowed in something like a lambda creation, it could be useful
> but it isn't!
>
> About the only thing that I can think of is if someone wishes to compress a
> file of python code a bit. The indentation can add up but a semi-colon does
> not solve all such problems.
>
> Would anything serious break if it was deprecated for use as a statement
> terminator? Then again, is it hurting anything? If it stopped being used
> this way, could it later be introduced as some new language feature or
> operator such as we now have a := b as a reuse of the colon, maybe a
> semicolon could be useful at least until someone decides to allow additional
> Unicode characters!
>
> Now if there are serious reasons to use semi-colon in python, great. If not,
> it is a historical artifact.
>
> -----Original Message-----
> From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
> Behalf Of Thomas Passin
> Sent: Wednesday, February 22, 2023 7:24 PM
> To: python-list@python.org
> Subject: Re: Introspecting the variable bound to a function argument
>
> On 2/22/2023 3:12 PM, Hen Hanna wrote:
>> On Wednesday, February 22, 2023 at 2:32:57 AM UTC-8, Anton Shepelev wrote:
>>> Hello, all.
>>>
>>> Does Python have an instrospection facility that can determine to
>>> which outer variable a function argument is bound, e.g.:
>>>
>>> v1 = 5;
>>> v2 = 5;
>>
>>
>> do some Python coders like to end lines with ; ?
>
> Very few, probably. It's not harmful but adds unnecessary visual clutter.
>
>>>
>>> def f(a):
>>> print(black_magic(a)) # or
> black_magic('a')
>>>
>>> f(v1) # prints: v1
>>> f(v2) # prints: v2
>>>
>>
>> the term [call by name] suggests this should be possible.
>>
>>
>> 30 years ago... i used to think about this type of thing A LOT ---
>> ------- CBR, CBV, CBN, (call by value), (call by name)....
> etc.
>>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

--
https://mail.python.org/mailman/listinfo/python-list
Re: semi colonic [ In reply to ]
On 23/02/2023 00:58, avi.e.gross@gmail.com wrote:
> So can anyone point to places in Python where a semicolon is part of a best
> or even good way to do anything?
>
>
Yes.  Take this bit of toy code which I just dreamed up.  (Of course it
is toy code; don't bother telling me how it could be written better.) 
If it looks a bit ragged, pretend it is in a fixed font.

if dow==0: day="Mon"; calcPay()
if dow==1: day="Tue"; calcPay()
if dow==2: day="Wed"; calcPay()
if dow==3: day="Thu"; calcPay()
if dow==4: day="Fri"; calcpay()
if dow==5: day="Sat"; calcPay(rate=1.5)
if dow==6: day="Sun"; calcPay(rate=2)

The point is: when you have several short bits of code with an identical
or similar pattern, *vertically aligning* the corresponding parts can
IMO make it much easier to read the code and easier to spot errors.
Compare this:

if dow==0:
    day="Mon"
    calcPay()
if dow==1:
    day="Tue"
    calcPay()
if dow==2:
    day="Wed"
    calcPay()
if dow==3:
    day="Thu"
    calcPay()
if dow==4:
    day="Fri"
    calcpay()
if dow==5:
    day="Sat"
    calcPay(rate=1.5)
if dow==6:
    day="Sun"
    calcPay(rate=2)

Not so easy to spot the mistake now, is it?
Not to mention the saving of vertical space.

Best wishes
Rob Cliffe
PS If you really care, I can send you a more complicated example of real
code from one of my programs which is HUGELY more readable when laid out
in this way.

--
https://mail.python.org/mailman/listinfo/python-list
Re: semi colonic [ In reply to ]
On 23/02/2023 02:04, Thomas Passin wrote:
> On 2/22/2023 7:58 PM, avi.e.gross@gmail.com wrote:
>>
>>
>> So can anyone point to places in Python where a semicolon is part of
>> a best
>> or even good way to do anything?
>
>  I use the semicolon (once in a while) is for quick debugging.  I
> might add as line like, perhaps,
>
> import os; print(os.path.exists(filename))
>
> This way I can get rid of the debugging statement by deleting that
> single line.  This is non only quicker but I'm less likely to delete
> too much by mistake.
>
I do exactly the same.
Rob Cliffe
--
https://mail.python.org/mailman/listinfo/python-list
Re: semi colonic [ In reply to ]
On Wednesday, February 22, 2023 at 6:21:13 PM UTC-8, Rob Cliffe wrote:
> On 23/02/2023 02:04, Thomas Passin wrote:
> > On 2/22/2023 7:58 PM, avi.e...@gmail.com wrote:
> >>
> >>
> >> So can anyone point to places in Python where a semicolon is part of
> >> a best
> >> or even good way to do anything?
> >
> > I use the semicolon (once in a while) is for quick debugging. I
> > might add as line like, perhaps,
> >
> > import os; print(os.path.exists(filename))
> >
> > This way I can get rid of the debugging statement by deleting that
> > single line. This is non only quicker but I'm less likely to delete
> > too much by mistake.
> >

> I do exactly the same.
> Rob Cliffe


i sometimes put extra commas... as:

[ 1, 2, 3, 4, ]

so it is (or may be) easier to add things later.

----------- i can think of putting extra final ; for the same reason.
--
https://mail.python.org/mailman/listinfo/python-list
RE: semi colonic [ In reply to ]
That seems like a reasonable if limited use of a semi-colon, Thomas.

Of course, most shells will allow a multi-line argument too like some AWK
scripts I have written with a quote on the first line followed by multiple
lines of properly formatted code and a closing quote.

Python though can get touchy about getting just the right amount of
indentation and simple attempts to break your program up into two lines

python -c "import sys
print('\n'.join(sys.path))"


DO not work so well on some shells.

So, yes, I agree. But I tried this on bash under Cygwin on windows using a
"here" document and it worked fine with multiple lines so something to
consider with no semicolons:

$ python <<!
> import sys
> print('\n'.join(sys.path))
> !

/usr/lib/python2.7/site-packages/pylint-1.3.1-py2.7.egg
/usr/lib/python2.7/site-packages/astroid-1.3.4-py2.7.egg
/usr/lib/python2.7/site-packages/six-1.9.0-py2.7.egg
/usr/lib/python27.zip
/usr/lib/python2.7
/usr/lib/python2.7/plat-cygwin
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/usr/lib/python2.7/site-packages
/usr/lib/python2.7/site-packages/gtk-2.0

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of Thomas Passin
Sent: Wednesday, February 22, 2023 9:05 PM
To: python-list@python.org
Subject: Re: semi colonic

On 2/22/2023 7:58 PM, avi.e.gross@gmail.com wrote:
> Thomas,
>
> This is one of many little twists I see between languages where one
> feature impacts use or even the need for another feature.
>
> So can anyone point to places in Python where a semicolon is part of a
> best or even good way to do anything?

Mostly I use it to run small commands on the command line with python -c.
e.g.

python -c "import sys;print('\n'.join(sys.path))"

This is handy enough that I wouldn't like to do without.

Another place I use the semicolon (once in a while) is for quick debugging.
I might add as line like, perhaps,

import os; print(os.path.exists(filename))

This way I can get rid of the debugging statement by deleting that single
line. This is non only quicker but I'm less likely to delete too much by
mistake.

> Some older languages had simple parsers/compilers that needed some way
> to know when a conceptual line of code was DONE and the semi-colon was
> a choice for making that clear. But some languages seem to only
> continue looking past an end-of-line if they detect some serious
> reason to assume you are in middle of something. An unmatched open
> parenthesis or square bracket might be enough, and in some languages a
curly brace.
>
> Python mainly has a concept of indentation and blank lines as one part
> of the guidance. Continuing lines is possible, if done carefully.
>
> But consider the lowly comma. Some languages may assume more is to
> come if it is dangled at the end of a line. But in a language that
> supports a dangling comma such as in making a tuple, how is the
> interpreter to know more is to come?
>
>>>> a = 5,
>>>> a
> (5,)
>
>>>> a = 5, \
> ... 6
>>>> a
> (5, 6)
>
> Well, one possible use of a semi-colon is to make short one-liner
> functions like this:
>
> def twoByFour(a): sq = a*a; forth = sq*sq; return((sq, forth))
>
> There is no reason, of course, that could not be done in multiple
> indented lines or other ways.
>
> So if it was allowed in something like a lambda creation, it could be
> useful but it isn't!
>
> About the only thing that I can think of is if someone wishes to
> compress a file of python code a bit. The indentation can add up but a
> semi-colon does not solve all such problems.
>
> Would anything serious break if it was deprecated for use as a
> statement terminator? Then again, is it hurting anything? If it
> stopped being used this way, could it later be introduced as some new
> language feature or operator such as we now have a := b as a reuse of
> the colon, maybe a semicolon could be useful at least until someone
> decides to allow additional Unicode characters!
>
> Now if there are serious reasons to use semi-colon in python, great.
> If not, it is a historical artifact.
>
> -----Original Message-----
> From: Python-list
> <python-list-bounces+avi.e.gross=gmail.com@python.org> On Behalf Of
> Thomas Passin
> Sent: Wednesday, February 22, 2023 7:24 PM
> To: python-list@python.org
> Subject: Re: Introspecting the variable bound to a function argument
>
> On 2/22/2023 3:12 PM, Hen Hanna wrote:
>> On Wednesday, February 22, 2023 at 2:32:57 AM UTC-8, Anton Shepelev
wrote:
>>> Hello, all.
>>>
>>> Does Python have an instrospection facility that can determine to
>>> which outer variable a function argument is bound, e.g.:
>>>
>>> v1 = 5;
>>> v2 = 5;
>>
>>
>> do some Python coders like to end lines with ; ?
>
> Very few, probably. It's not harmful but adds unnecessary visual clutter.
>
>>>
>>> def f(a):
>>> print(black_magic(a)) # or
> black_magic('a')
>>>
>>> f(v1) # prints: v1
>>> f(v2) # prints: v2
>>>
>>
>> the term [call by name] suggests this should be possible.
>>
>>
>> 30 years ago... i used to think about this type of thing A LOT ---
>> ------- CBR, CBV, CBN, (call by value), (call by
name)....
> etc.
>>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

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

--
https://mail.python.org/mailman/listinfo/python-list
Re: semi colonic [ In reply to ]
On 2/22/2023 10:42 PM, avi.e.gross@gmail.com wrote:
> That seems like a reasonable if limited use of a semi-colon, Thomas.
>
> Of course, most shells will allow a multi-line argument too like some AWK
> scripts I have written with a quote on the first line followed by multiple
> lines of properly formatted code and a closing quote.

"Most shells"... got to include cmd.exe, don't forget.

> Python though can get touchy about getting just the right amount of
> indentation and simple attempts to break your program up into two lines
>
> python -c "import sys
> print('\n'.join(sys.path))"
>
>
> DO not work so well on some shells.
>
> So, yes, I agree. But I tried this on bash under Cygwin on windows using a
> "here" document and it worked fine with multiple lines so something to
> consider with no semicolons:
>
> $ python <<!
>> import sys
>> print('\n'.join(sys.path))
>> !
>
> /usr/lib/python2.7/site-packages/pylint-1.3.1-py2.7.egg
> /usr/lib/python2.7/site-packages/astroid-1.3.4-py2.7.egg
> /usr/lib/python2.7/site-packages/six-1.9.0-py2.7.egg
> /usr/lib/python27.zip
> /usr/lib/python2.7
> /usr/lib/python2.7/plat-cygwin
> /usr/lib/python2.7/lib-tk
> /usr/lib/python2.7/lib-old
> /usr/lib/python2.7/lib-dynload
> /usr/lib/python2.7/site-packages
> /usr/lib/python2.7/site-packages/gtk-2.0
>
> -----Original Message-----
> From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
> Behalf Of Thomas Passin
> Sent: Wednesday, February 22, 2023 9:05 PM
> To: python-list@python.org
> Subject: Re: semi colonic
>
> On 2/22/2023 7:58 PM, avi.e.gross@gmail.com wrote:
>> Thomas,
>>
>> This is one of many little twists I see between languages where one
>> feature impacts use or even the need for another feature.
>>
>> So can anyone point to places in Python where a semicolon is part of a
>> best or even good way to do anything?
>
> Mostly I use it to run small commands on the command line with python -c.
> e.g.
>
> python -c "import sys;print('\n'.join(sys.path))"
>
> This is handy enough that I wouldn't like to do without.
>
> Another place I use the semicolon (once in a while) is for quick debugging.
> I might add as line like, perhaps,
>
> import os; print(os.path.exists(filename))
>
> This way I can get rid of the debugging statement by deleting that single
> line. This is non only quicker but I'm less likely to delete too much by
> mistake.
>
>> Some older languages had simple parsers/compilers that needed some way
>> to know when a conceptual line of code was DONE and the semi-colon was
>> a choice for making that clear. But some languages seem to only
>> continue looking past an end-of-line if they detect some serious
>> reason to assume you are in middle of something. An unmatched open
>> parenthesis or square bracket might be enough, and in some languages a
> curly brace.
>>
>> Python mainly has a concept of indentation and blank lines as one part
>> of the guidance. Continuing lines is possible, if done carefully.
>>
>> But consider the lowly comma. Some languages may assume more is to
>> come if it is dangled at the end of a line. But in a language that
>> supports a dangling comma such as in making a tuple, how is the
>> interpreter to know more is to come?
>>
>>>>> a = 5,
>>>>> a
>> (5,)
>>
>>>>> a = 5, \
>> ... 6
>>>>> a
>> (5, 6)
>>
>> Well, one possible use of a semi-colon is to make short one-liner
>> functions like this:
>>
>> def twoByFour(a): sq = a*a; forth = sq*sq; return((sq, forth))
>>
>> There is no reason, of course, that could not be done in multiple
>> indented lines or other ways.
>>
>> So if it was allowed in something like a lambda creation, it could be
>> useful but it isn't!
>>
>> About the only thing that I can think of is if someone wishes to
>> compress a file of python code a bit. The indentation can add up but a
>> semi-colon does not solve all such problems.
>>
>> Would anything serious break if it was deprecated for use as a
>> statement terminator? Then again, is it hurting anything? If it
>> stopped being used this way, could it later be introduced as some new
>> language feature or operator such as we now have a := b as a reuse of
>> the colon, maybe a semicolon could be useful at least until someone
>> decides to allow additional Unicode characters!
>>
>> Now if there are serious reasons to use semi-colon in python, great.
>> If not, it is a historical artifact.
>>
>> -----Original Message-----
>> From: Python-list
>> <python-list-bounces+avi.e.gross=gmail.com@python.org> On Behalf Of
>> Thomas Passin
>> Sent: Wednesday, February 22, 2023 7:24 PM
>> To: python-list@python.org
>> Subject: Re: Introspecting the variable bound to a function argument
>>
>> On 2/22/2023 3:12 PM, Hen Hanna wrote:
>>> On Wednesday, February 22, 2023 at 2:32:57 AM UTC-8, Anton Shepelev
> wrote:
>>>> Hello, all.
>>>>
>>>> Does Python have an instrospection facility that can determine to
>>>> which outer variable a function argument is bound, e.g.:
>>>>
>>>> v1 = 5;
>>>> v2 = 5;
>>>
>>>
>>> do some Python coders like to end lines with ; ?
>>
>> Very few, probably. It's not harmful but adds unnecessary visual clutter.
>>
>>>>
>>>> def f(a):
>>>> print(black_magic(a)) # or
>> black_magic('a')
>>>>
>>>> f(v1) # prints: v1
>>>> f(v2) # prints: v2
>>>>
>>>
>>> the term [call by name] suggests this should be possible.
>>>
>>>
>>> 30 years ago... i used to think about this type of thing A LOT ---
>>> ------- CBR, CBV, CBN, (call by value), (call by
> name)....
>> etc.
>>>
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

--
https://mail.python.org/mailman/listinfo/python-list
Re: semi colonic [ In reply to ]
On Wed, 22 Feb 2023 18:25:00 -0800 (PST), Hen Hanna wrote:


> i sometimes put extra commas... as:
>
> [ 1, 2, 3, 4, ]
>
> so it is (or may be) easier to add things later.


That can bite you with things like JSON that aren't very forgiving. The
same can be said for single quotes that may or may not work as intended.
--
https://mail.python.org/mailman/listinfo/python-list
Re: semi colonic [ In reply to ]
On 23/02/23 1:58 pm, avi.e.gross@gmail.com wrote:

> Would anything serious break if it was deprecated for use as a statement
> terminator?

Well, it would break all the code of people who like to
write code that way. They might get a bit miffed if we
decide that their code is not serious. :-)

On the other hand, if they really want to, they will still
be able to abuse semicolons by doing this sort of thing:

a = 5; pass
b = 7; pass
c = a * b; pass

Then everyone will know it's some really serious code!

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list
Re: semi colonic [ In reply to ]
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
RE: semi colonic [ In reply to ]
Greg,

How did you know that was the method I used to indicate I had properly
debugged and tested a line of code?

a = 5; pass
b = 7; pass
c = a * b; pass

Then I switched to using comments:

a = 5 # pass
b = 7 # pass
c = a * b # fail

And would you believe it still worked!

OK, I am just kidding if anyone is taking this seriously.


-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of Greg Ewing via Python-list
Sent: Thursday, February 23, 2023 1:28 AM
To: python-list@python.org
Subject: Re: semi colonic

On 23/02/23 1:58 pm, avi.e.gross@gmail.com wrote:

> Would anything serious break if it was deprecated for use as a
> statement terminator?

Well, it would break all the code of people who like to write code that way.
They might get a bit miffed if we decide that their code is not serious. :-)

On the other hand, if they really want to, they will still be able to abuse
semicolons by doing this sort of thing:

a = 5; pass
b = 7; pass
c = a * b; pass

Then everyone will know it's some really serious code!

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list
Re: semi colonic [ In reply to ]
Thomas Passin wrote at 2023-2-22 21:04 -0500:
>On 2/22/2023 7:58 PM, avi.e.gross@gmail.com wrote:
> ...
>> So can anyone point to places in Python where a semicolon is part of a best
>> or even good way to do anything?
>
>Mostly I use it to run small commands on the command line with python
>-c. e.g.
>
>python -c "import sys;print('\n'.join(sys.path))"
>
>This is handy enough that I wouldn't like to do without.
>
>Another place I use the semicolon (once in a while) is for quick
>debugging. I might add as line like, perhaps,
>
>import os; print(os.path.exists(filename))

I also see, `;` occasionally in `*.pth` files.
--
https://mail.python.org/mailman/listinfo/python-list
RE: semi colonic [ In reply to ]
That is a reasonable use, Rob, albeit I would refactor that example in quite a few ways so the need for a semicolon disappears even for lining things up.

So to extrapolate, perhaps a related example might be as simple as wanting to initialialize multiple variables together might suffice as in:

if dow == 0: hours_worked = 8; overtime = False

Of course some monstrosities are now possible for such a scenario such as


if dow == 0: hours_worked, overtime = 8, False

Not that readable.

I repeat, there is nothing wrong with a language having a feature like a semi-colon even if it is mainly syntactic sugar. Just wondering if it was widely used or even essential. My thought was that python evolved when some languages really needed a terminator but as it went another way, using indentation and sometimes blank lines, ...

I am not sure what Dieter meant about seeing semicolons in .pth files. I expect to see them in all kinds of files containing python code or anything created with a structure that chooses to include it.

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On Behalf Of Rob Cliffe via Python-list
Sent: Wednesday, February 22, 2023 9:11 PM
To: python-list@python.org
Subject: Re: semi colonic



On 23/02/2023 00:58, avi.e.gross@gmail.com wrote:
> So can anyone point to places in Python where a semicolon is part of a
> best or even good way to do anything?
>
>
Yes. Take this bit of toy code which I just dreamed up. (Of course it is toy code; don't bother telling me how it could be written better.) If it looks a bit ragged, pretend it is in a fixed font.

if dow==0: day="Mon"; calcPay()
if dow==1: day="Tue"; calcPay()
if dow==2: day="Wed"; calcPay()
if dow==3: day="Thu"; calcPay()
if dow==4: day="Fri"; calcpay()
if dow==5: day="Sat"; calcPay(rate=1.5)
if dow==6: day="Sun"; calcPay(rate=2)

The point is: when you have several short bits of code with an identical or similar pattern, *vertically aligning* the corresponding parts can IMO make it much easier to read the code and easier to spot errors.
Compare this:

if dow==0:
day="Mon"
calcPay()
if dow==1:
day="Tue"
calcPay()
if dow==2:
day="Wed"
calcPay()
if dow==3:
day="Thu"
calcPay()
if dow==4:
day="Fri"
calcpay()
if dow==5:
day="Sat"
calcPay(rate=1.5)
if dow==6:
day="Sun"
calcPay(rate=2)

Not so easy to spot the mistake now, is it?
Not to mention the saving of vertical space.

Best wishes
Rob Cliffe
PS If you really care, I can send you a more complicated example of real code from one of my programs which is HUGELY more readable when laid out in this way.

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

--
https://mail.python.org/mailman/listinfo/python-list
Re: semi colonic [ In reply to ]
On 2023-02-23, rbowman <bowman@montana.com> wrote:
> On Wed, 22 Feb 2023 18:25:00 -0800 (PST), Hen Hanna wrote:
>
>> i sometimes put extra commas... as:
>>
>> [ 1, 2, 3, 4, ]
>>
>> so it is (or may be) easier to add things later.
>
> That can bite you with things like JSON that aren't very forgiving.

Oh, how I hate that about JSON...


--
https://mail.python.org/mailman/listinfo/python-list
RE: semi colonic [ In reply to ]
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@python.org> On Behalf Of Rob Cliffe via Python-list
Sent: Thursday, February 23, 2023 6:08 AM
To: python-list@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

--
https://mail.python.org/mailman/listinfo/python-list
RE: semi colonic [ In reply to ]
Grant,

I am not sure it is fair to blame JSON for a design choice.

Use of commas can be done many ways in many contexts.

One context is a sort of placeholder. Can you have a language where a
function has multiple arguments and you can skip some as in:

Func(a,b,c)
Func(a, b,)
Func(a,,)

Or even
Func(a,,c)

The missing arguments in such a language may be viewed as some sort of NULL
or take a default value as a possibility.

So what if you have a language where a list or tuple or other data structure
incorporates a similar idea when created or used. If I have a matrix and I
want every entry in row 4, meaning all columns, can I ask for mat[4,] and it
means something else than mat[4] which may return a vector instead of a
matrix?

There are tons of such ideas that are choices. Python allows a SINGLE comma
here but multiple are an error:

>>> a=1
>>> a
1
>>> a=1,
>>> a
(1,)
>>> a=1,,
SyntaxError: incomplete input

So why not allow MULTIPLE commas and ignore them? It is a choice!

Here is a scenario where a trailing comma is an error:

>>> a,b,,, = range(5)
SyntaxError: invalid syntax
>>> a,b,_,_,_ = range(5)
>>> a,b,*_ = range(5)

The way to deal here with more items is to use * in front of the last one to
gather any strays.

But as _ is simply reused in the middle example and meant to be ignored, why
do you need it if you would simply allow multiple commas? Short answer is
they did not choose to design it that way. The places in python that do
allow a trailing "," will allow only one. Beyond that, they assume you are
making an error. So if someone wants to make a list of 5 things in
alphabetical order but forgets a few, they cannot write:

mylist = [first, , third, , , ]

and then let the code run to be enhanced later with their reminder. What
they can do is write this:

mylist = [first,
#,
third,
#,
#,
]

The reminders are now simply well-placed comments.

Now we could debate the design of JSON and some enhancements people have
made for other more portable data structures. I think it reasonable that
they decided to stick to working with fully-formatted data structures and
guess what? If I make a list or tuple or other data structures in python
with a trailing comma, it is NOT stored that way and if you display it,
there is no trailing comma shown. It is fully JSON compatible in some sense:

>>> import json
>>> mynest = [1,2, [3, 4,], 5,]
>>> mynest
[1, 2, [3, 4], 5]
>>> json.dumps(mynest)
'[1, 2, [3, 4], 5]'
>>> json.dumps([1,2, [3, 4,], 5,])
'[1, 2, [3, 4], 5]'
>>> json.loads(json.dumps(mynest))
[1, 2, [3, 4], 5]

So when are you running into problems? Is it when reading something from a
file using a function expecting properly formatted JSON?





-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of Grant Edwards
Sent: Thursday, February 23, 2023 2:28 PM
To: python-list@python.org
Subject: Re: semi colonic

On 2023-02-23, rbowman <bowman@montana.com> wrote:
> On Wed, 22 Feb 2023 18:25:00 -0800 (PST), Hen Hanna wrote:
>
>> i sometimes put extra commas... as:
>>
>> [ 1, 2, 3, 4, ]
>>
>> so it is (or may be) easier to add things later.
>
> That can bite you with things like JSON that aren't very forgiving.

Oh, how I hate that about JSON...


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

--
https://mail.python.org/mailman/listinfo/python-list
Re: semi colonic [ In reply to ]
On Fri, 24 Feb 2023 at 06:29, Grant Edwards <grant.b.edwards@gmail.com> wrote:
>
> On 2023-02-23, rbowman <bowman@montana.com> wrote:
> > On Wed, 22 Feb 2023 18:25:00 -0800 (PST), Hen Hanna wrote:
> >
> >> i sometimes put extra commas... as:
> >>
> >> [ 1, 2, 3, 4, ]
> >>
> >> so it is (or may be) easier to add things later.
> >
> > That can bite you with things like JSON that aren't very forgiving.
>
> Oh, how I hate that about JSON...
>

Also C#, it's incredibly frustrating that I'm not allowed to have a
trailing comma in a function's argument list. Ugh. Completely
unnecessary restriction.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: semi colonic [ In reply to ]
On 24/02/23 9:26 am, avi.e.gross@gmail.com wrote:
> Python One-Liners: Write Concise, Eloquent Python Like a Professional Illustrated Edition
> by Christian Mayer (Author)

I didn't know there were any Professional Illustrated Editions
writing Pythom. You learn something every day! :-)

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list
Re: semi colonic [ In reply to ]
On Fri, 24 Feb 2023 at 17:36, Greg Ewing via Python-list
<python-list@python.org> wrote:
>
> On 24/02/23 9:26 am, avi.e.gross@gmail.com wrote:
> > Python One-Liners: Write Concise, Eloquent Python Like a Professional Illustrated Edition
> > by Christian Mayer (Author)
>
> I didn't know there were any Professional Illustrated Editions
> writing Pythom. You learn something every day! :-)
>

"I don't want to be a moving picture in a book!" -- Bert, locomotive
on the Small Railway

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: semi colonic [ In reply to ]
On 2023-02-23 15:56:54 -0500, avi.e.gross@gmail.com wrote:
> I am not sure it is fair to blame JSON for a design choice.

We can't blame JSON (it has no agency), but as you say, it it was a
choice. And we can absolutely blame Doug for making that choice!

hp

--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"