[Python-ideas] Customizing format()

Terry Reedy tjreedy at udel.edu
Wed Mar 18 02:50:14 CET 2009

Raymond Hettinger wrote:
> [Terry Reedy]
>> I strongly prefer suffix to prefix modification.  
> Given the way that the formatting parsers are written,
> I think suffix would work just as well as prefix.  Also,
> your idea may help with the mental parsing as well
> (because the rest of the format string uses the untranslated symbols so 
> that translation pairs should
> be at the end).
>>> Also, this version makes it easy to employ a couple of techniques to 
>>> factor-out
>> These techniques apply to any "augment the basic format with an
>> affix" method.
> Right.
>> I also prefer, mnemonic key letters to mark the start of each affix, 
> ...
>> format(1234, '8.1f;T.;P,')

This should have been

format(1234, '8.1f;T.;D,')

> I think it's better to be explicit that periods are translated to commas
> and commas to periods.  Introducing a new letter just adds more to
> more memory load and makes the notation more verbose.  In the
> previous newgroup discussions, people reacted badly to letter
> mnemonics finding them to be so ugly that they would refuse to use them 
> (remember the early proposal of format(x,"8T,.f)).
> Also, the translation pairs approach lets you swap other hardwired
> characters like the E or a 0 pad.

So does the key letter approach.  The pairs approach does not allow easy 
alteration of the grouping spec, because there is no hard-wired char to 
swap, unless you would allow something cryptic like {3,(4,2,3)) (for 
India, I believe).

Even with the tranlation pair, one could use a separator rather than fences.

format(1234, '8.1f;T.;D,') # could be

format(1234, '8,.1f;,.;.,')

The two approachs could even be mixed by using a char only when clearer, 
such as 'G' for grouping instead of '3' for the existing grouping value. 
  I think whatever scheme adopted should be complete.

Terry Jan Reedy

More information about the Python-ideas mailing list