[Python-ideas] Descouraging the implicit string concatenation

Steven D'Aprano steve at pearwood.info
Wed Mar 14 10:18:06 EDT 2018


On Wed, Mar 14, 2018 at 02:31:58PM +0100, Oleg Broytman wrote:

[...]
> > fruits = {
> >     "apple",
> >     "orange"
> >     "banana",
> >     "melon",
> > }
> 
>    I am all for that, with both hands! +1000000, dammit!
> 
>    Once I stumbled over a bug caused by this in legacy code in
> production. Fixing it was quite painful!

Did you mean that *finding* the bug was painful? Fixing it should be 
trivially easy: add a comma.

But honestly, I'm having trouble seeing why it would be painful to find 
the bug. As soon as you see "orangebanana" where you expected "orange" 
or "banana", that should give you a very strong clue. But without 
knowing the details of your code, and how convoluted the problem was, I 
can't really judge. I can only say that in *my personal experience* 
finding and fixing these sorts of errors is usually simple.


>    The problem with the idiom is it's too easy to make the mistake and
> rather hard to find.

I disagree that its easy to make or hard to find, but I accept that 
not everyone will have the same experience as me. For those who have 
this problem, perhaps you should use a linter to check your code and 
warn about string concatenation.


-- 
Steve


More information about the Python-ideas mailing list