[Python-Dev] Re: PEP 292, Simpler String Substitutions
Christian Tismer
tismer@tismer.com
Sun, 23 Jun 2002 22:51:41 +0200
Paul Prescod wrote:
> Christian Tismer wrote:
>
>>...
>>
>>Are you sure you got what I meant?
>>I want to compile the variable references away at compile
>>time, resulting in an ordinary format string.
>>This string is wraped by the runtime _(), and
>>the result is then interpolated with a dict.
>
>
> How can that be?
>
> Original expression:
>
> _($"$foo")
>
> Expands to:
>
> _("%(x1)s"%{"x1": foo})
>
> Standard Python order of operations will do the %-interpolation before
> the method call! You say that it could instead be
>
> _("%(x1)s")%{"x1": foo}
>
> But how would Python know to do that? "_" is just another function.
> There is nothing magical about it. What if the function was instead
> re.compile? In that case I would want to do the interpolation *before*
> the compilation, not after!
>
> Are you saying that the "_" function should be made special and
> recognized by the compiler?
As you say it, it looks a little as if something special
would be needed, right.
I have no concrete idea.
Somehow I'd want to express that a function is
applied after compile time substitution, but before
runtime interpolation.
Here a simple idea, while not very nice, but it could work:
Assume a "$" prefix, which does the interpolation in the
way you said.
Assume further a "%" prefix, which does it only halfway,
returning a tuple: (modified string, dict).
This tuple would be passed to _(),
and it is _()'s decision to work this way:
def _(s):
if type(s) == type(()):
s, args = s
else:
args = None
#... processing s ...
if args:
return s % args
else:
return s
But this is a minor issue, I just wanted to tell what
I think should happen, without giving an exact
solution.
cheers - chris
--
Christian Tismer :^) <mailto:tismer@tismer.com>
Mission Impossible 5oftware : Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a : *Starship* http://starship.python.net/
14109 Berlin : PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34 home +49 30 802 86 56 pager +49 173 24 18 776
PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04
whom do you want to sponsor today? http://www.stackless.com/