[Python-ideas] string.Template v2

Ben Finney ben+python at benfinney.id.au
Fri Nov 29 04:15:38 CET 2013


"Stephen J. Turnbull" <stephen at xemacs.org>
writes:

> But when you don't have a TABLE element available for formatting
> tables, width, precision, base, and the like are *necessary*
> information for nice output. Do you really think it's readable to
> write
>
> "I propose a fine of ${payment: type=float precision=2} for not reading the docs.".format(payment=100)
>
> vs.
>
> "I propose a fine of ${payment:.2f} for not reading the docs.".format(payment=100)

The first is far more readable, because it is explicit and doesn't
require special knowledge to know what the parameters are.

An uninformed guess by a casual reader is much more likely to be right
in the first case; I think that's an important criterion for
readability.

Which is not to say I *prefer* the first one. I think readability can be
sacrificed in some cases where the benefit of concision is high, and a
template paramter format is one of those cases IMO.

But we need to acknowledge that concision and readability are sometimes
conflicting goals. I think it's clear that your examples illustrate that
conflict.

> -1 on adding more templating features to Python's stdlib; .format()
> already hits the sweet spot given current best practice IMO.

Definitely agreed.

-- 
 \        “Always code as if the guy who ends up maintaining your code |
  `\     will be a violent psychopath who knows where you live.” —John |
_o__)                                                         F. Woods |
Ben Finney



More information about the Python-ideas mailing list