[Python-ideas] String formatting and namedtuple

Aahz aahz at pythoncraft.com
Tue Feb 10 18:03:19 CET 2009

On Tue, Feb 10, 2009, Lie Ryan wrote:
> I've been experimenting with namedtuple, it seems that string formatting 
> doesn't recognize namedtuple as mapping. 
> from collections import namedtuple
> Nt = namedtuple('Nt', ['x', 'y'])
> nt = Nt(12, 32)
> print 'one = %(x)s, two = %(y)s' % nt
> # output should be:
> one = 12, two = 32
> currently, it is possible to use nt._asdict() as a workaround, but I 
> think it will be easier and more intuitive to be able to use namedtuple 
> directly with string interpolation

This makes perfect sense to me; I think you should feel free to go ahead
and submit a patch.  (Regardless of whether the patch gets accepted, at
least we would have a good record -- python-ideas is not really a good
place to record ideas, and this is something nice and simple.)
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote 
programs, then the first woodpecker that came along would destroy civilization.

More information about the Python-ideas mailing list