why no automatic conversion in string concatenation?

Chris Mellon arkanes at gmail.com
Tue Nov 13 14:18:31 EST 2007


On Nov 13, 2007 1:09 PM, Michael Pelz Sherman <mpelzsherman at yahoo.com> wrote:
> Thanks Cliff. Not to belabor this point - clearly it's just something I'll
> have to get used to - but isn't the choice of the "+" as the concatenation
> operator part of the problem here? A more "explicit" choice would have been
> an "append()" function, would it not? Or at least a non-ambiguous character.
>
> Obviously python *does* know that I'm concatenating, not adding, due to the
> type of the LH object, and it would save me a lot of time if I didn't have
> to explicitly convert my objects to strings.
>

Python doesn't *know* that. It could assume that, and it could
document that assumption (and you could overload + to do that if you
wanted in your own classes), but it chooses not to assume that - you
know best what you want to do. A second operator could be used, but
since there's no ambiguity (and Python won't guess, so incorrect use
is always an error) there doesn't seem to be much gain from doing so.

It's quite rare that this actually involves much extra code - you
probably want to be using string interpolation (which will convert to
str() automatically if you use the %s format character) instead.



More information about the Python-list mailing list