yet another recipe on string interpolation
Raymond Hettinger
vze4rx4y at verizon.net
Mon Nov 8 14:47:34 EST 2004
"Michele Simionato" <michele.simionato at gmail.com> wrote in message
news:4edc17eb.0411080106.71d4f3d4 at posting.google.com...
> Peter Otten <__peter__ at web.de> wrote in message
news:<cmdpe5$uem$02$1 at news.t-online.com>...
> > As of 2.4b2, the modulo operator is not (not yet, or no longer?) defined, so
> > I had to use substitute() instead.
>
> I have downloaded Python 2.4b2 today and looked at the source code for
> the Template class. There is no __mod__ method, in contraddiction with
> PEP 292. Is there somebody who knows what happened and the rationale
> for the change? Should PEP 292 be updated?
It was updated a good while ago:
http://www.python.org/peps/pep-0292.html
The PEP STATES that the API is through the substitute() and safe_substitute()
methods. Also, it notes that __mod__ is easily aliased to one of the those via
a Template subclass.
By using named methods instead of the % operator:
* we avoid the precedence issues associated with %
* we avoid confusion as to whether a tuple is an acceptable second argument
* we avoid confusion with "%s" style inputs.
* we enable keyword arguments: mytempl.substitute(weather="rainy")
* we get something more documenting than punctuation.
Raymond Hettinger
>
> Michele Simionato
More information about the Python-list
mailing list