[Python-ideas] Descouraging the implicit string concatenation

Steven D'Aprano steve at pearwood.info
Wed Mar 14 11:33:45 EDT 2018


On Wed, Mar 14, 2018 at 03:01:07PM +0100, Oleg Broytman wrote:

> > People can make all sorts of mistakes through carlessness. I wrote
> 
> >     {y, x*3}
> > 
> > the other day instead of {y: x**3}. (That's *two* errors in one simple
> > expression. I wish I could say it was a record for me.) Should we
> > "discourage" exponentiation and dict displays and insist on writing
> > dict((y, x*x*x)) to avoid the risk of errors? I don't think so.
> 
>    We should fix what causes real problems, not convoluted ones. And
> this particular misfeature caused problems for me.

I don't think that using a single asterisk instead of two is a 
"convoluted" problem.

Please Oleg, if you want others to respect your personal experiences, 
you should return the favour and respect theirs. Don't try to dismiss 
them as "convoluted" or not "real". I am far more likely to mistype 
exponentiation with a single asterisk than to accidentally leave out a 
comma from a list display of strings.

But I recognise that this is *my* problem, not the language.

You want some sort of "official" recommendation not to use implicit 
string concatenation. What benefit will that give you, except to nag
those who like and use the feature?

- It won't give you a error or warning when your code accidentally uses
string concatenation; you still need to be alert for the possibility, or 
use a linter.

- It won't teach people not to use this feature; you still need to point 
them at the recommendation and have them read it and agree to follow it.

- It won't automatically remove string concatenation from the code you 
use, nor prevent it from happening again, or even change your project 
style-guide.

- It won't even add any information to the community that doesn't 
already exist.

I have no doubt there are many blog posts and Stackoverflow discussions 
about this. I know I've talked about it many times on comp.lang.python. 
If you know how to google, you can find a post from Guido talking about 
getting "a mysterious argument count error" due to this.

If you want something to point people at, bookmark Guido's comment.

But for that matter, what will this statement say?

    "Don't use implicit concatenation. Even though this is not
    deprecated and will remain legal, you mustn't use it because
    you might use it in a list or function call and forget to
    use a comma between two string literals and get a mysterious
    error."

Um, yeah, okay, I get lots of mysterious errors caused by carelessness. 
Some of them are easy to fix. Some are much harder to track down and fix 
than a missing comma. Do they all need official warnings in the docs?

    "Don't misspell None as 'none' in three dozen places. And if
    you do, don't spend twenty minutes fixing them one at a time,
    use your editor's search and replace function."

(That really was not my best day.)

I don't think this is any worse or more common than any other gotcha 
or silly error that might trip people up occasionally, and I think that 
singling it out in the Python docs for special warning is unnecessary.


-- 
Steve


More information about the Python-ideas mailing list