Draft PEP: string interpolation with backquotes
Fernando Pérez
fperez528 at yahoo.com
Sun Dec 2 13:13:54 EST 2001
phil hunt wrote:
>>>> "x is $x, f(x) is $f(x)"
>>>>
>>>>This to me is readable, unambiguous and very useful.
>>>
>>> To me it implies the same as:
>>>
>>> "x is %(x)s, f(x) is %(f)s(x)" % { 'x': x, 'f': f}
>>>
>>No.
>
> Wrong. Read what I said closely. I am right on this, beleive me.
Well, no. But no point arguing, here's code to prove it means what I
said, not what you did:
In [1]: from Itpl import itpl
In [2]: def f(x): return x**2
...:
In [3]: x=5
Here's what I said it meant, $f(x) would evaluate f(x):
In [4]: itpl('x is $x, f(x) is $f(x)')
Out[4]= 'x is 5, f(x) is 25'
And here's your expression, copied verbatim:
In [5]: "x is %(x)s, f(x) is %(f)s(x)" % { 'x': x, 'f': f}
Out[5]= 'x is 5, f(x) is <function f at 0x80e2824>(x)'
Not the same thing. Ping's Itpl module is, IMHO, *very* well done.
>> $_whatever_ means: fully evaluate _whatever_, as far as it can be
>>evaluated, and put its value in there. So it gives an interpolation
>>syntax which is in fact much cleaner than perl's, with much less
>>need for braces and name protection.
>
> If you want Perl-like features, you can always use Perl.
What's wrong in this group every time someone suggests something that
even remotely resembles perl there's a fanatic response to it? Cool
down. I've used perl a lot, and am sick of it. I don't want python to
become perl, but I simply keep an open mind to situations where in
real, everyday work, I find it lacking.
And I think the beauty of an open language is precisely that these
discussions can be had. But obviously if everytime someone suggests
something different (even if they are wrong) your response is to tell
them 'if you don't like it get out of here', progress isn't exactly
going to be very fast...
> Easy:
>
> "some %s expression" % really_complex_expression()
>
> What's wrong with that?
Nothing, for *one* expression. What if there's 50 evaluations that
need to be done? As I said, *scalability* of the tool is the issue
here.
>
> Anyway, having complex espressions interleaved with static text,
> whether you do it the Perl way or the C++ way, makes the code less
> readable, IMO.
Or in certain cases, more readable. I'm not thinking of writing web
pages here (which I know zilch about). But I often write code which
analyzes a lot of scientific data and generates text file summaries
of it. It's vastly clearer to be able to write:
" Pos: ($i,$j,$k), Dist = $sqrt(i**2+j**2+k**2), Corr = $C[i][j][k]"
than *any* of the existing alternatives in python. Here it is:
In [23]: itpl(" Pos: ($i,$j,$k), Dist = $sqrt(i**2+j**2+k**2), \
....: Corr = $C[i][j][k]")
Out[23]= ' Pos: (1,2,3), Dist = 3.74165738677, Corr = 5'
I read that line and I can know exactly what it's doing, what it
evaluates and what data it will show. And I can dig out examples
where things are 100 times more complicated than this.
> I've re-read your whole post, and still don't see any problem. You
> mention long strings with lots of lines and lots of values to fill
> in. I've done that with the % oprator, with no problems. I disagree
> that using a temporary variable here is a problem, to me it doesn't
> reduce clarity at all.
Let's see if the example I gave above shines some light on this.
Think of a case like that, multiply by 100, and now spend three days
building your table of temporary variables to make it work. No
thanks. That's why I want clean string interpolation.
Anyway, I'm trying to push the discussion simply because I've yet to
see an argument that convinces me it's a bad idea. But I can always
keep on using the Itpl module as I have until now.
I honestly think python would be better off with the feature. But
that's just my opinion, nothing else.
Cheers,
f.
More information about the Python-list
mailing list