Lists And Missing Commas

Avi Gross avigross at verizon.net
Tue Dec 24 13:02:03 EST 2019


As a purist, it makes people uncomfortable if all 'objects' are not treated
alike.

But I look at the question from a definition and parsing mechanism view.

When an interpreter (or compiler) reads a program, it often does it in
phases and tries to tokenize parts.

So, the definition of something often makes it look ahead or back or even
sideways to see how much of the program text makes up a single entity. 

The definition of a NUMBER is complex but clearly it needs to allow
underscores or designations that make it octal versus decimal or a floating
point number or even scientific notation. The point is it does some looking
ahead to make sure it gets the entire number and then sets it aside as a
token for a single entity.

The definition of a fixed string is also complex as it can be a binary
string or other formats but the focus here is that string literals were
DESIGNED to be able to span multiple lines or be put in piecewise. So the
parser does not stop when it sees "Hello " but continues to see if there is
more that can be used to make a full string literal. It has to skip past
whitespace and see what follows. If what follows is "World!" then it makes
"Hello World!" and keeps scanning beyond more whitespace until something is
found that cannot be considered part of the same literal. Other languages
use this technique such as C++ 

An analogy might be how a compiler in a language that supports constants
will look at a line declaring something like this:

	const int volume = 5 * 6 * 7

Would it not be possible that the compiler, knowing this is a constant,
would not pre-calculate the result and act as if you had written:

	const int volume = 210

The run time, would not know or care.

So, if the analogy makes sense, in python the early evaluation phase is
required to combine as much as possible into a single string literal. It is
a bit like a regular expression that matches a pattern greedily unless asked
not to. This tokenization probably precedes the point where it sees a series
of commas in a context such as a list definition or function call argument
list. 

Again, a purist might want all objects, within reason, to look alike. There
are other mechanisms to gain this concatenation functionality in strings.
This being Python (which lies about how there should be one unique way to
logically do something) there may be dozens of ways to get this
functionality starting with using a PLUS operator and continuing with one of
5 or so ways to build a string from other objects as well as strategic uses
of the backslash character and so on.

But it is a feature that is there and seems to also be in some other
languages. It is generally easy to avoid using this implicit concatenation
but I think an important point is that you may inadvertently get the wrong
result when accidentally leaving out a comma. That is true. I note this is
an old known issue:

https://www.python.org/dev/peps/pep-3126/

There are some lint programs that check your code and supply warnings and I
see some languages have the option to generate warnings when the two strings
are on the same line. I wonder if a Python lint does that. It may at least
warn of this usage in time to check the code and put back the comma.


-----Original Message-----
From: Python-list <python-list-bounces+avigross=verizon.net at python.org> On
Behalf Of Tim Daneliuk
Sent: Monday, December 23, 2019 11:22 PM
To: python-list at python.org
Subject: Re: Lists And Missing Commas

On 12/23/19 8:35 PM, Chris Angelico wrote:
> On Tue, Dec 24, 2019 at 12:56 PM DL Neil via Python-list 
> <python-list at python.org> wrote:
>> However, your point involves the fact that whereas:
>>
>> 1 + 2           # 3 is *clearly* addition, and
>> "a" + "b"       # "ab" is *clearly* concatenation
>>
>> "a" "b"         # also evaluates to "ab"
>>
>> and is thus, concatenation without any explicit infix operator! Just 
>> one cotton-picking minute - what's going on here?
> 
> 1_2    # Clearly concatenation. Right?
> 0_0    # Clearly an emoticon
> 
> Just another cotton-picking minute......
> 
> ChrisA
> 

You are excused and required to do 30 days penance writing .NET.
--
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list