问题
I am running on Python 3.6.1 and today I missed a comma, as follows:
nt = namedtuple('Record', ['c', 'a' 'b'])
# instead the following is what I actually want
nt = namedtuple('Record', ['c', 'a', 'b'])
But I just wonder why the first way is valid Python in any way? Should not it complains with syntax error?
I just tried in 3.5.2 and 2.7.11. Seems all valid. But it is valid?
回答1:
From the Python tutorial:
Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated.
>>> 'Py' 'thon' 'Python'
回答2:
Python will concatenate adjacent strings which are delimited by whitespace: https://docs.python.org/2.0/ref/string-catenation.html
来源:https://stackoverflow.com/questions/50698086/strange-valid-python-syntax-with-string