[Python-ideas] allow line break at operators

Georg Brandl g.brandl at gmx.net
Sun Sep 18 12:46:21 CEST 2011


Am 18.09.2011 12:22, schrieb Ben Finney:
> Arnaud Delobelle <arnodel at gmail.com>
> writes:
> 
>> On 4 September 2011 23:39, Ben Finney <ben+python at benfinney.id.au> wrote:
>> > Ben Finney <ben+python at benfinney.id.au> writes:
>> >
>> >> How many string literals do you count in the following statement? I
>> >> count one:
>> >>
>> >>     raise HoustonWeHaveAProblemError(
>> >>         "Lorem ipsum dolor sit amet,"
>> >>         " consectetur adipiscing elit.")
>>
>> The code object says that there's one string constant in the compiled
>> function. It says nothing (and knows nothing) about the number of
>> string literals that made up this string.
> 
> Hmm. So how should I be looking to answer the question? My AST-fu is
> weak.

It is sufficient to look into Grammar/Grammar:

atom: ('(' [yield_expr|testlist_comp] ')' |
       '[' [listmaker] ']' |
       '{' [dictorsetmaker] '}' |
       '`' testlist1 '`' |
       NAME | NUMBER | STRING+)

The little "+" after STRING tells you taht the multiple string literals
survive the tokenizer and are concatenated during AST creation (look
for parsestrplus() in Python/ast.c).

Georg




More information about the Python-ideas mailing list