[Python-Dev] summary of transitioning from % to {} formatting

Vinay Sajip vinay_sajip at yahoo.co.uk
Mon Oct 5 23:46:52 CEST 2009

Nick Coghlan <ncoghlan <at> gmail.com> writes:

> Oh, I see what you meant now - you were pointing out that lazy
> formatting APIs (such as logging) already don't work properly for
> alternative formatting mechanisms (such as string.Template).

Logging doesn't work automatically with string.Template as it pre-dates
string.Template, but it can be made to work without too much trouble:

(a) Subclass Formatter to use $-formatting instead of %-formatting for e.g. the
lines written to log files, and
(b) Use a class like DollarMessage which I mentioned elsewhere in this thread,
to format logging call format strings and arguments.

Both of these approaches will also work for {}-formatting. The present thread
really started out with a view to suggesting that the stdlib start adopting
{}-format as "native", rather than %-format.

Would it be helpful if I added a section to the Python docs about how to use
alternative formatting systems with logging?


Vinay Sajip

More information about the Python-Dev mailing list