[Python-ideas] Implicit string literal concatenation considered harmful?

MRAB python at mrabarnett.plus.com
Sat May 11 18:55:43 CEST 2013


On 11/05/2013 04:37, Nick Coghlan wrote:
> On Sat, May 11, 2013 at 10:29 AM, Bruce Leban <bruce at leapyear.org> wrote:
>> I got bit by this quite recently, leaving out a comma in a long list of
>> strings and I only found the bug by accident.
>>
>> This being python "ideas" I'll throw one out.
>>
>> Add another prefix character to strings:
>>
>>     a = [m'abc'
>>          'def']   # equivalent to ['abcdef']
>
> As MRAB suggested, a prefix for a compile time dedent would likely be
> more useful - then you'd just use a triple quoted string and be done
> with it. The other one I occasionally wish for is a compile time
> equivalent of str.split (if we had that, we likely wouldn't see APIs
> like collections.namedtuple and enum.Enum accepting space separated
> strings).
>
> Amongst my ideas-so-farfetched-I-never-even-wrote-them-up (which is
> saying something, given some of the ideas I *have* written up) is a
> notation like:
>
>      !processor!"STRING LITERAL"
>
> Where the compile time string processors had to be registered through
> an appropriate API (probably in the sys module). Then you would just
> define preprocessors like "merge" or "dedent" or "split" or "sh" of
> "format" and get the appropriate compile time raw string->AST
> translation.
>
> So for this use case, you would do:
>
>      a = [!merge!"""\
>             abc
>             def"""
>
Do you really need the "!"? String literals can already have a prefix,
such as "r".

At compile time, the string literal could be preprocessed according to
its prefix (some kind of import hook, working on the AST?). The current
prefixes are "" (plain literal), "r", "b", "u", etc.



More information about the Python-ideas mailing list