Lists And Missing Commas

DL Neil PythonList at DancesWithMice.info
Mon Dec 23 20:52:41 EST 2019


On 24/12/19 1:48 PM, Tim Daneliuk wrote:
> If I do this:
> 
>      foo = [ "bar", "baz" "slop", "crud" ]
> 
> Python silently accepts that and makes the middle term "bazslop".
> 
> BUT, if I do this:
> 
>      foo = [ "bar", "baz" 1, "crud" ]
> 
> or this:
> 
>      foo = [ "bar", 2 1, "crud" ]
> 
> The interpreter throws a syntax error.
> 
> This is more of an intellectual curiosity than anything else, but why do strings silently
> concatenate like that, while all other case blow up with an error.  i.e., What is about
> the language the promotes this behavior.  At first blush, it seems inconsistent, but what
> do I know ...

Restricting our conversation to numbers/integers and strings, 
"concatenate" only ever seems to apply to string objects.
(there are meanings of "concatenate" for matrices, for example)

For fun, try:

help( str )	# and
help( int )

They both mention __add__(self, value, /) In both cases such is defined 
as "self+value" - but remember that the "+" operator is "over-loaded". 
Thus (and I'm telling you nothing new here) when dealing with strings we 
read "+" as "concatenate"; whereas with integers it is plainly "add". 
Two quite different operators/operations!


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?

The answer to all your dreams (um, maybe...) lies in "The Python 
Language Reference" docs, particularly Section 2 "Lexical Analysis" (the 
process of taking "tokens" and making sense of how they go-together!).

<<<
2.4.2. String literal concatenation
Multiple adjacent string or bytes literals (delimited by whitespace), 
possibly using different quoting conventions, are allowed, and their 
meaning is the same as their concatenation. Thus, "hello" 'world' is 
equivalent to "helloworld". This feature can be used to reduce the 
number of backslashes needed, to split long strings conveniently across 
long lines, or even to add comments to parts of strings, for example:

re.compile("[A-Za-z_]"       # letter or underscore
            "[A-Za-z0-9_]*"   # letter, digit or underscore
           )

Note that this feature is defined at the syntactical level, but 
implemented at compile time. The ‘+’ operator must be used to 
concatenate string expressions at run time. Also note that literal 
concatenation can use different quoting styles for each component (even 
mixing raw strings and triple quoted strings), and formatted string 
literals may be concatenated with plain string literals.
 >>>


Thus, the adjacency of two literals explicitly only implies 
concatenation for strings. There is no equivalent/similar mention for 
numbers.


WebRef: https://docs.python.org/3/reference/lexical_analysis.html
-- 
Regards =dn


More information about the Python-list mailing list