[Python-ideas] Descouraging the implicit string concatenation

Søren Pilgård fiskomaten at gmail.com
Wed Mar 14 08:43:53 EDT 2018


On Wed, Mar 14, 2018 at 1:18 PM, Facundo Batista
<facundobatista at gmail.com> wrote:
> Hello!
>
> What would you think about formally descouraging the following idiom?
>
>     long_string = (
>         "some part of the string "
>         "with more words, actually is the same "
>         "string that the compiler puts together")
>
> We should write the following, instead:
>
>     long_string = (
>         "some part of the string " +
>         "with more words, actually is the same " +
>         "string that the compiler puts together")
>
> I know that "no change to Python itself" is needed, but having a
> formal discouragement of the idiom will help in avoiding people to
> fall in mistakes like:
>
> fruits = {
>     "apple",
>     "orange"
>     "banana",
>     "melon",
> }
>
> (and even making the static analysers, like pyflakes or pylint, to
> show that as a warning)
>
> Note that there's no penalty in adding the '+' between the strings,
> those are resolved at compilation time.
>
> Thanks!!
>

I agree that implicit concatenation is a bad feature. I have seen it
cause multiple stupid errors while I have never actually used it for
anything useful.
I have also experienced beginners asking why you can do `x = "abc"
"def"` but not `a = "abc"; b = "def"; x = a b` and then you have to
either explain them the differences between strings and string
literals or just tell them to always use `+`.

I think it breaks multiple parts of the zen of python:
Explicit is better than implicit.
Special cases aren't special enough to break the rules.
Errors should never pass silently.
There should be one-- and preferably only one --obvious way to do it.


More information about the Python-ideas mailing list