[Python-ideas] Descouraging the implicit string concatenation

Robert Vanden Eynde robertve92 at gmail.com
Wed Mar 14 10:51:22 EDT 2018


+1 on general idea of discouraging it.

> [...] mistakes like:
>
> fruits = {
>     "apple",
>     "orange"
>     "banana",
>     "melon",
> }

+1

> (and even making the static analysers, like pyflakes or pylint, to
> show that as a warning)

+1

> 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. [...] Zen of Python.

+1. But I think a counter argument would be a real world example.

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

Maybe the "+" at the beginnig as PEP8 said, to clearly mark it present
and explicit.

     long_string = (
         "some part of the string "
         + "with more words, actually is the same "
         + "string that the compiler puts together")

or

     long_string = (""
         + "some part of the string "
         + "with more words, actually is the same "
         + "string that the compiler puts together")


> People can make all sorts of mistakes through carlessness. I wrote
>    {y, x*3}
> the other day instead of {y: x**3}

Other comments said this isn't really the same kind of error, I agree to them.

>> Note that there's no penalty in adding the '+' between the strings,
>> those are resolved at compilation time.

> That is an implementation feature, not a language requirement. Not all
> Python interpreters will do that, and they are free to put limits on
> how much they optimize. Here's Python 3.5:

> With string concatentation having potential O(N**2) performance, if
> you're writing explicit string concatenations with +, they could
> potentially be *very* expensive at runtime.

I completely agree, by emitting warning if this syntax is used, codes
Could be less efficient, but that's also the job of the
implementations to be fast.
This is the only counter-argument I have to OP, even if I really like
the proposal.

> Life is too short to learn *everything*, I know that, and I've certainly
> copied lots of code I don't understand (and hoped I'd never need to
> debug it!). If that makes me a cargo-cult coder too, so be it.

I find that learning `x = "e" "f"` does indeed compile is also
learning and surprising (especially for beginners).

> While I personally dislike implicit string concatenation,
> I've worked in projects that use it, and I, more often than
> I'd like, use them myself.

We would like to see more real world example vs real world mistakes.

> It is a bit  cumbersome to do something like
> from textwrap import dedent as D
> a = D("""\
>       My code-aligned, multiline text
>       with no spaces to the left goes here
>       """)

+1. But you should add "\n" to have

    a = ("hello\n"
         "world\n"
         "how")

Which would be maybe better written as

    a = '\n'.join([
         "hello",
         "world",
         "how"])

(put the spaces where you like)

which can easily be switched to ''.join if needed.

> A common problem is that you have something like
> foo(["a",
>      "b",
>      "c"
> ]).bar()
> but then you remember that there also needs to be a "d" so you just add

+1

> Yes of course the skilled programmer needs to understand the
> difference. But I am talking about absolute beginners where python in
> many regards is an excelent first language.

Agreed.

2018-03-14 15:30 GMT+01:00 Stephan Houben <stephanh42 at gmail.com>:
>
>
> Op 14 mrt. 2018 15:23 schreef "Facundo Batista" <facundobatista at gmail.com>:
>
>
> I propose the discouragement of the idiom.
>
>
>
> What does that mean?
>
> Stephan
>
>
> Regards,
>
> --
> .    Facundo
>
> Blog: http://www.taniquetil.com.ar/plog/
> PyAr: http://www.python.org/ar/
> Twitter: @facundobatista
>
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>


More information about the Python-ideas mailing list