[Python-Dev] transitioning from % to {} formatting (original) (raw)

Vinay Sajip vinay_sajip at yahoo.co.uk
Thu Oct 1 18:21:00 CEST 2009


Raymond Hettinger <python rcn.com> writes:

It looks like the BraceMessage would have to re-instantiate on every invocation.

True, because the arguments to the instantiation are kept around as a BraceMessage instance until the time comes to actually format the message (which might be never). Since typically in performance-sensitive code, the isEnabledFor pattern is used to avoid doing unnecessary work, as in

if logger.isEnabledFor(logging.DEBUG): logger.debug(__("The {0} is {1}", "answer", 42))

The BraceMessage invocation overhead is only incurred when needed, as is the cost of computing the additional arguments.

As I understand it {}-formatting is slower than %-formatting anyway, and if this pattern is used only for {}-formatting, then there will be no additional overhead for %-formatting and some additional overhead for {}-formatting. I'm not sure what that instantiation cost will be relative to the overall time for an "average" call - whatever that is ;-) - though.

Other approaches to avoid instantiation could be considered: for example, making __ a callable which remembers previous calls and caches instances keyed by the call arguments. But this will incur memory overhead and some processing overhead and I'm not sure if it really buys you enough to warrant doing it.

Regards,

Vinay Sajip



More information about the Python-Dev mailing list