string interpolation for python

Yingjie Lan lanyjie at yahoo.com
Mon Apr 2 03:39:42 EDT 2012


> You can already do essentially that without adding a special-case string 

> formatting method to the general methods we already have.
> 
>>>>  balls = 5
>>>>  people = 3
>>>>  'The {people} people have {balls} 
> balls.'.format(**locals())
> 'The 3 people have 5 balls.'


Clearly dynamic strings are much more powerful,
allowing arbitrary expressions inside. It is also
more terse and readable, since we need no dictionary.

I would probably rather liken dynamic expressions
as a little brother of computable documents in 
Mathematica. It is a new kind of expression,
rather than formatting -- though it has formatting
connections. 

Dynamic strings are mainly useful at time of
writing readable code before compilation. 
The compiler can choose to convert it into
a string formatting expression, of course.
To efficiently format strings at runtime, 
the best choice (especially
for safty reasons) is string formatting, 
not evaluating a dynamic string.

On the implementation, I would suppose new 
syntax is needed (though very small).

Yingjie



More information about the Python-list mailing list