[Python-Dev] Replacement for print in Python 3.0

Nick Coghlan ncoghlan at gmail.com
Wed Sep 7 14:55:43 CEST 2005


Barry Warsaw wrote:
> On Wed, 2005-09-07 at 05:23, Stephen J. Turnbull wrote:
> 
> 
>>But print-ng looks
>>like becoming the OOWTDI for a lot of applications.  IMO it's just too
>>early to give up on print-ng becoming the one obvious way to do it for
>>a lot of i18n apps, too.
> 
> 
> +1.  I have a gut feeling that we can make it easy for monolinguists to
> use printng without caring or even knowing about i18n, but also make it
> relatively painless to integrate i18n into an application or library. 
> However I haven't had time to really explore that idea.

I found the following to be an interesting experiment:

-------------
from string import Template

def format(*args, **kwds):
        fmt = args[0]
        kwds.update(("p%s" % idx, arg) for idx, arg in enumerate(args))
        return Template(fmt).substitute(**kwds)

Py> format("$p1: $p2", "Bee count", 0.5)
'Bee count: 0.5'
-------------

The leading 'p' (for 'positional') is necessary to get around the fact that $1 
is currently an illegal identifier in a Template. If we actually did something 
like this, I would advocate adding the support for positional arguments 
directly to string.Template.

For il8n output, you would be pulling the format string from somewhere else, 
so you would stick with the current idiom of using keyword arguments:

-------------
Py> fmt = "$item: $count"

Py> format(fmt, item="Bee count", count=0.5)
'Bee count: 0.5'
-------------

There's also the 
cute-and-kinda-useless-but-it-also-justifies-the-1-based-indexing:

-------------
Py> format("Kinda cute: $p0")
'Kinda cute: Kinda cute: $p0'
-------------

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.blogspot.com


More information about the Python-Dev mailing list