[Python-ideas] String formatting and namedtuple
Aahz
aahz at pythoncraft.com
Wed Feb 11 16:38:55 CET 2009
On Tue, Feb 10, 2009, Aahz wrote:
> 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.)
Actually, I just realized that this can't work because then you couldn't
have
print 'one = %s, two = %s' % nt
due to the overloading on __getitem__(). Even if you could somehow
special-case this, tuple index sequencing needs to continue working as
fast path.
--
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