string interpolation for python

Yingjie Lan lanyjie at yahoo.com
Mon Apr 2 07:46:41 EDT 2012


> 

>>>>  "Are you "+name+"?"
> 
> That allows arbitrary expressions and everything.
> 

To make that work for any type, you need:

>>> "Are you "+ str(name) + "?"

Another concern is performance.

You are absolutely right, they are 
equivalent in that both are expressions.
As long as people start to realize that
dynamic strings are expressions,
there is no magic in it any more.

And allowing expressions in those
dynamic strings would make sense 
since they are of the same sort.

>>> d"sin($x$) = $ sin(x):0.3f $"

is equivalent to the expression of

>>> "sin(%s"%x + ")= %0.3f"%sin(x)

Comparing th e two, I would say the latter
is more computer friendly while 
the former, more human friendly.

If the computed result is only to be
used in formatting the string, it would
be nice to save an assignment stmt.


>> 
>>  Almost as terse, but not as readable, especially
>>  when there are many parts to substitute --
>>  the coder and reader need to be careful
>>  to make sure the sequence is correct.
> 
> I quite like this notation, personally. It's convenient, and is
> supported (with variants) in quite a few C-derived languages (and, in
> spite of the massive syntactic differences, Python does have C
> heritage).

Sure, once you get used to it, it would be harder to stop it
 the harder it is :). That's part of human nature, anyway.


>>  Why the Python community is so
>>  hostile to new things now?
>>  Python has merits,
>>  but it is far from being perfect.
> 
> Hey now, no need to get defensive :) Thing is, it's up to you to
> demonstrate that your proposal justifies itself. You're proposing to
> create a massive backward-compatibility issue, so you need to prove
> that your new way of formatting strings is sufficiently awesome to be
> able to say "Well, you need Python 3.4+ to use this".
> 


OK. I have put it out as is. I trust people knows good things.

I would simply say: this new way is much more simple 
and much more powerful. And there is no security issues
as long as you don't use the evil eval to evaluate expressions,
which is always a security issue.

It is new, and has no compatibility issues with old ways at all.
In syntax, all you need is to allow d"...", which clearly won't
affect any old ways of business.

Cheers,

Yingjie



More information about the Python-list mailing list