formatting (was Re: Perl is worse!)

Bernhard Herzog herzog at online.de
Mon Jul 31 12:56:44 EDT 2000


"Alex Martelli" <alex at magenta.com> writes:

> Python isn't beyond criticism here, by the way.  As an
> alternative to the % _operator_, a named-function (to
> be provided as a built-in) could perfectly well have been
> chosen by our BDFL:
> 
>     format(formatstring, a, b, c)
> 
> or
> 
>     format(formatstring, vars())
> 
> isn't (IMHO) any less readable than the current
> 
>     formatstring % (a,b,c)
> 
> or
> 
>     formatstring % vars()
> 
> just _slightly_ more verbose, but, again IMHO, that slight
> extra verbosity would be in a good cause.

A function also has the advantage that it could be extended more easily.
The % operator has only two arguments which doesn't leave much room for
new features.

In Sketch, for example, I've implemented a format function that supports
user defined conversions:

      format(template, dict, converters)

The template string can contain conversions of the form
	%(key)[converter] 
which is replaced by the value of
	converters["converter"](dict["key"])

The graphics objects in Sketch which don't know anything about user
preferences for units supply both the template and dict and the UI code
provides the converters for lenghts and positions so that e.g. the
template "%(width)[length]" and the dict {'width': 72} are displayed as
"72 pt" or "2.54 cm" depending on user preferences. This setup
coventiently puts all knowledge about the preferred units in one place.


-- 
Bernhard Herzog   | Sketch, a drawing program for Unix
herzog at online.de  | http://sketch.sourceforge.net/



More information about the Python-list mailing list