[Python-3000] string.Formatter class

Eric Smith eric+python-dev at trueblade.com
Wed Aug 29 01:18:24 CEST 2007


Jim Jewett wrote:
> On 8/28/07, Eric Smith <eric+python-dev at trueblade.com> wrote:
>> parse(format_string)
> 
>> ... returns an iterable of tuples
>> (literal_text, field_name, format_spec, conversion)
> 
> Which are really either
> 
>     (literal_text, None, None, None)
> or
>     (None, field_name, format_spec, conversion)
> 
> I can't help thinking that these two return types shouldn't be
> alternatives that both pretend to be 4-tuples.  At the least, they
> should be
> 
>     "literal text"
> vs
>     (field_name, format_spec, conversion)

I agree that it might not be the best interface.  It was originally just 
an internal thing, where it didn't really matter.  But since then it's 
(possibly) become exposed as part of the Formatter API, so rethinking it 
makes sense.  I really didn't want to write:

for result in parse(format_string):
     if isinstance(result, str):
         # it's a literal
     else:
         field_name, format_spec, conversion = result

> but you might want to take inspiration from the "tail" of an
> elementtree node, and return the field with the literal next to it as
> a single object.
> 
>     (literal_text, field_name, format_spec, conversion)

I think I like that best.

Thanks!

Eric.


More information about the Python-3000 mailing list