A simple single line, triple-quoted comment is giving syntax error. Why?

Chris Angelico rosuav at gmail.com
Sun Mar 22 00:11:53 EDT 2015


On Sun, Mar 22, 2015 at 2:49 PM, Thomas 'PointedEars' Lahn
<PointedEars at web.de> wrote:
>> Implicit concatenation is part of the syntax, not part of the expression
>> evaluator.
>
> Reads like nonsense to me.

What do you mean? String concatenation by abuttal is as much a
syntactic element as the distinction between regular, raw, and
triple-quoted string literals. By the time you get to AST (never mind
about byte code), that information is gone:

>>> print(ast.dump(ast.parse("""
... x = '''hello'''
... """)))
Module(body=[Assign(targets=[Name(id='x', ctx=Store())], value=Str(s='hello'))])
>>> print(ast.dump(ast.parse("""
... x = r'hello'
... """)))
Module(body=[Assign(targets=[Name(id='x', ctx=Store())], value=Str(s='hello'))])
>>> print(ast.dump(ast.parse("""
... x = "hello"
... """)))
Module(body=[Assign(targets=[Name(id='x', ctx=Store())], value=Str(s='hello'))])
>>> print(ast.dump(ast.parse("""
... x = "he" "ll" "o"
... """)))
Module(body=[Assign(targets=[Name(id='x', ctx=Store())], value=Str(s='hello'))])

Nothing in the expression evaluator knows or cares about what kind of
string literal you used, nor whether you included more than one. It's
all just alternative forms of string literal.

ChrisA



More information about the Python-list mailing list