[issue5205] String Formatting with namedtuple

Lie Ryan report at bugs.python.org
Tue Feb 10 16:04:25 CET 2009


New submission from Lie Ryan <lie.1296 at gmail.com>:

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.

I tried this with 2.6 and 2.7a (trunk). Haven't seen 3.0 yet.

PS: Although the following works, it is not the topic of the issue:
print 'one = %s, two = %s' % nt

----------
components: Library (Lib)
messages: 81555
nosy: lieryan
severity: normal
status: open
title: String Formatting with namedtuple
type: feature request
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5205>
_______________________________________


More information about the Python-bugs-list mailing list