|
|
|
|
|
by Skeime
1023 days ago
|
|
I strongly disagree. Not converting the int to a string automatically is absolutely the right decision. In all code I write, this TypeError would catch an actual error, because concatenation of strings is just not the right tool for creating "abc123" from "abc" and 123, so I would not use it for that. Hence, if this exception occurs, it indicates that I probably mixed up variables somewhere. Use one of the (admittedly too) many string formatting tools that Python offers, for example an f-string like f"abc{123}". (Also, if you have enough type annotations in your code, the type checker will warn you about these, so you can fix them before they hit testing or even production.) |
|
Now, I guess I'm not against and explicit cast and I can imagine how the error could catch an actual bug. It's painful when the error stops the execution when the string concatenation was intended, but it is not really an issue anymore with the possibility to type check before the execution.
> concatenation of strings is just not the right tool for creating "abc123" from "abc" and 123
Why? This sounds like an opinion to me. String interpolation of formatting features are nice but I find them quite clunky in such simple cases.
Of course when you have to be careful to call str(val), it's arguably as clunky...