In a dynamic language, why % operator asks user for type info?
marduk at nbk.hopto.org
Tue Jul 17 03:06:27 CEST 2007
On Mon, 2007-07-16 at 17:33 -0700, Karthik Gurusamy wrote:
> Thanks. The above surprised me as I didn't expect that %s will accept
> Looks like the implicit conversion doesn't work the other way.
> >>> '%s' % 42
> >>> '%d' % '42'
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: int argument required
> Looks like %s can be used even when I'm sending non-strings.
> >>> '%s foo %s bar' % (25, 25.34)
> '25 foo 25.34 bar'
> So %s seems to serve the multi-type placeholder.
According to the docs: http://docs.python.org/lib/typesseq-strings.html
By design, %s "converts any python object using str()". OTOH it does
not specify that %d, for example, calls int().
More information about the Python-list