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

Nick Coghlan ncoghlan at gmail.com
Sat May 11 05:37:04 CEST 2013


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

Cheers,
Nick.

--
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list