[Python-Dev] string formatting (extended print statement, uPre-PEP)

M.-A. Lemburg mal@lemburg.com
Mon, 24 Jul 2000 12:53:27 +0200


Ka-Ping Yee wrote:
> 
> On Sun, 23 Jul 2000, Paul Prescod wrote:
> > As happy as I am to have it called Prescod-ese, I admit to influence
> > from some languages that (otherwise) suck. :)
> 
> I agree that formatting and interpolation is one area where Perl is
> much stronger, and it should be easy to do such things in Python.
> 
> > credit-where-due 'ly yrs
> 
> I kind of hate to do this, but i can't restrain myself from pointing
> out that i *did* propose just this solution quite recently, in:
> 
>     http://www.python.org/pipermail/python-dev/2000-July/012764.html
> 
> I wrote a module to do this a few years ago (1996, i think).  It
> supported attribute lookup, item lookup, and expressions.  (No one
> has yet mentioned the issue of escaping the dollar sign, which i
> handled by doubling it.)
> 
> See the exact rules described, with examples, at
> 
>     http://www.lfw.org/python/Itpl.html
> 
> If you want to play, just download
> 
>     http://www.lfw.org/python/Itpl15.py
> 
> and try the itpl() function in the module.
> 
> I would be quite pleased if (for example) $"" performed the
> equivalent of a function call to itpl() on the enclosed string.

Any reason this can't be emulated using the existing
"..." % mapping syntax ?

You'd just have to provide a smart mapping which evaluates
the %(something)s tags in the format string to a string
ready for insertion... something may even contain balanced
parenthesis, so even function calls are within range.

Note that

a = 1
b = 2
print "We have %(a)s apples and %(b)s oranges" % locals()

is already possible today without any additional magic.

-- 
Marc-Andre Lemburg
______________________________________________________________________
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/