[Python-ideas] Implicit string literal concatenation considered harmful?
Christian Tismer
tismer at stackless.com
Sat May 11 19:05:05 CEST 2013
On 11.05.13 05: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"""
>
Ah, I see we are on the same path here.
Just not sure if it is right to move into a compile-time preprocessor
language or to just handle the most common cases with a simple
prefix?
One example is code snippets which need proper de-indentation.
I think a simple stripping of white-space in
text = s"""
leftmost column
two-char indent
"""
would solve 95 % of common indentation and concatenation cases.
I don't think provision for merging is needed very often.
If text occurs deeply nested in code, then it is also quite likely to
be part of an expression, anyway.
My major use-case is text constants in a class or function that
is multiple lines long and should be statically ready to use without
calling a function.
(here an 's' as a strip prefix, but I'm not sold on that)
cheers - chris
--
Christian Tismer :^) <mailto:tismer at stackless.com>
Software Consulting : Have a break! Take a ride on Python's
Karl-Liebknecht-Str. 121 : *Starship* http://starship.python.net/
14482 Potsdam : PGP key -> http://pgp.uni-mainz.de
phone +49 173 24 18 776 fax +49 (30) 700143-0023
PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04
whom do you want to sponsor today? http://www.stackless.com/
More information about the Python-ideas
mailing list