Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax quirks or why is that valid python

Tags:

python

syntax

In python 2.6, why is the following line valid?

my_line = 'foo' 'bar'

and if that is valid, why isn't the following:

my_list = 1 2 

The first example is string concatenation, however, the following isn't valid either (thanks god):

foo = 'foo'
bar = 'bar'
foo_bar = foo bar
like image 867
Boris Gorelik Avatar asked Dec 21 '09 15:12

Boris Gorelik


2 Answers

This is doing string literal concatenation. As noted in the documentation, advantages include the following:

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...

It goes on to note that this concatenation is done at compilation time rather than run time.

The history and rationale behind this, and a rejected suggestion to remove the feature, is described in PEP 3126.

like image 183
Peter Hansen Avatar answered Sep 30 '22 14:09

Peter Hansen


my_line = 'foo' 'bar' is string concatenation.

like image 42
jldupont Avatar answered Sep 30 '22 13:09

jldupont