By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,097 Members | 1,580 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,097 IT Pros & Developers. It's quick & easy.

issue with string.Template

P: n/a
This is somewhat in between a bug report and a feature request.
I was using the new string.Template class and I have run into a
few issues that I traced back to the usage of the idiom

'%s' % val

in the 'convert' helper function in Template.substitute.

I do not understand why '%s' % val was used instead of just
str(val).

'%s' % val in unfortunate since it results in the following
surprising (for me) behavior:
from string import Template as T T("$obj").substitute(obj=("hello",)) 'hello'

[not '("hello",)']
T("$obj").substitute(obj=()) TypeError: not enough arguments for format string

[not '()']

Is this intended behavior? It is surprising since it is
different from what one would expect from old-fashioned string
interpolation
"%(obj)s" % dict(obj=("hello",)) "('hello',)" "%(obj)s" % dict(obj=())

'()'

which is the behaviour I find most useful. Also, I do not like
that different expressions such as
T("$obj").substitute(obj=("hello",))
and T("$obj").substitute(obj="hello") give the same output
(this potentially hides type bugs).

So, take this as a bug report if the behavior is not intended and
as a feature request if the current behaviour is the intended
one ;)

Michele Simionato

P.S. at the end, the problem is that string interpolation with
positional arguments is somewhat of a hack, but fixing this will
have to wait until Python 3000 ...

Sep 11 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Michele Simionato wrote:
from string import Template as T T("$obj").substitute(obj=())
TypeError: not enough arguments for format string

So, take this as a bug report if the behavior is not intended and
as a feature request if the current behaviour is the intended
one ;)


I vote for bug report. The need to habitually wrap any tuple arguments into
another 1-tuple is clearly at odds with the goal to simplify string
interpolation.

Peter
Sep 11 '05 #2

P: n/a
Michele Simionato wrote:
This is somewhat in between a bug report and a feature request.
I was using the new string.Template class and I have run into a
few issues that I traced back to the usage of the idiom

'%s' % val

in the 'convert' helper function in Template.substitute.

I do not understand why '%s' % val was used instead of just
str(val).
The reason is written in the code comments:

"""
# We use this idiom instead of str() because the latter will
# fail if val is a Unicode containing non-ASCII characters.
"""
P.S. at the end, the problem is that string interpolation with
positional arguments is somewhat of a hack, but fixing this will
have to wait until Python 3000 ...
The plan for Py3.0 is to have a formatting function that doesn't have
the same tuple vs scalar issue.
So, take this as a bug report if the behavior is not intended and
as a feature request if the current behaviour is the intended
one ;)


Feel free to post a SF report. If Barry wants to alter the behavior,
it is easy enough to do:

try:
return str(s)
except UnicodeEncodeError:
return unicode(s)
Raymond Hettinger

Sep 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.