[Python-ideas] String interpolation again.

Mike Meyer mwm-keyword-python.b4bdba at mired.org
Fri Jul 23 15:37:05 CEST 2010

On Fri, 23 Jul 2010 15:25:42 +0200
Masklinn <masklinn at masklinn.net> wrote:

> On 2010-07-23, at 15:05 , Mike Meyer wrote:
> > The first problem with this kind of thing is that there's no obvious
> > reason why 12 + '34' should be '1234' instead of 46.
> > 
> > Java variables have declared types. This means the above situation can
> > be detected at compile time, and the implicit conversion added
> > then. In Python, you have to do the tests at run time, which will slow
> > everything down.
> Actually, it's much simpler than that for Java: the `+` operator is specially overloaded any time a string is involved to become a "convert and concatenate" operator similar to PHP's "." rather than the usual "add" operator.
> In Python, the equivalent behavior would be to special case the addition operator so that it checks if either operand is a string and if it is convert and concatenate the other one, otherwise apply normal resolution.

I would hope the Java version isn't as convoluted as you say (but
given Java, it may be): all this really requires is that the string
version of + include the conversion. In python, that would be making
str.__add__ (and friends) do the conversion. Given that in Python,
str(a_string) is a_string, and doing the type check on a string (via
either type or isinstance) is about the same speed as calling str on
it (just under .2 usecs/loop), you might as well just always do the
conversion. Or maybe this breaks unicode...

Assuming, of course, you actually think this is a good idea.

Mike Meyer <mwm at mired.org>		http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

More information about the Python-ideas mailing list