[Python-3000] String formating operations in python 3k

Barry Warsaw barry at python.org
Mon Apr 3 18:59:56 CEST 2006


On Mon, 2006-04-03 at 11:13 -0500, Ian Bicking wrote:

> assert path.startswith(prefix), (
>      "%r should start with %r" % (path, prefix))
> assert path.startswith(prefix), (
>      $"${repr(path)} should start with ${repr(prefix)}")
> assert path.startswith(prefix), (
>      "$path should start with $prefix".substitute(
>      path=repr(path), prefix=repr(prefix))

The second example is why I added sys._getframe().  In my i18n code, I
wanted to do things like this (rewritten for $-string style):

print _("$user is not a member of the $listname mailing list")

and it would Just Work.  Now, the trick is that in the same function,
just before the print line, you'd have code like this (pseudocoded):

user = utils.websafe(form.get('user'))
listname = mlist.get_name()

So _() does a sys._getframe() to dig the locals out of the calling
function, and does the substitutions from there.  So, if your
applications isn't i18n, I could see the use for a format() global, but
while I'd probably want the behavior I just described, you might not, so
I'm not sure we could write a format() builtin that would be useful to
us both.

-Barry

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 309 bytes
Desc: This is a digitally signed message part
Url : http://mail.python.org/pipermail/python-3000/attachments/20060403/84dbfb43/attachment.pgp 


More information about the Python-3000 mailing list