[Python-ideas] String interpolation again.

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

On Fri, 23 Jul 2010 08:35:40 -0400
Alex Light <scialexlight at gmail.com> wrote:

> On Fri, Jul 23, 2010 at 8:26 AM, Andrey Popp <8mayday at gmail.com> wrote:
> > I think it's a form of weak typing from languages like PHP, that
> > allows programmer to make huge amounts of mistakes, that are sometimes
> > difficult to spot.

It also violates TOOOWTDI.

> I agree. but i wouldn't mind if python would start automatically calling str
> on objects in string sequences if they are not strings.
> i.e. turn this
> not_a_string = 123
> a_string = 'this is a string' + not_a_string
> #currently throws syntax error
>  to this:
> a_string = 'this is a string ' + str(not_a_string)
> they do this in java and it works quite well

I believe the initial quote applies to this form as well.  It might
work well in Java, but Java isn't Python; there are lots of other
differences that make this a lot more tolerable in Java.

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.

Further, Java's typed variables means that if you've made a mistake in
the type of one of the values, the assignment will be flagged as an
error at compile time. Python won't do that, so implicitly fixing the
mistake here means you get even further from the error before
something happens that reveals it.

Finally, the % operator does that implicit conversion for you if you
use %s:
  a_string = 'this is a string %s' % not_a_string
Works just fine as things are now (though the syntax needs tweaking if
not_a_string is a tuple).

I think brings it to -4.

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