Take 2: PEP draft for expression embedding
oren-py-l at hishome.net
Mon Dec 17 13:15:30 CET 2001
On Sun, Dec 16, 2001 at 01:31:52PM +2328, Fernando Pérez wrote:
> Sorry for being lazy, but since you've already done the search homework,
> would you mind pointing to Frederik's post on i()? I've never seen it and I'm
> quite intrigued.
It's ridiculously, why-didn't-we-think-of-this-before-ly simple:
i = lambda *args: ''.join(map(str,args))
print i("X=",x," Y=",calc_y(x))
The argument list of this function behaves just like the argument list of
the print statement, the only difference is that no spaces are added
automatically. IMHO, it's much more friendly than % formatting.
A version of this function implemented in C should be about as fast as
formatting with the % operator and almost two orders of magnitude faster
than any form of interpolation that scans for expressions inside a string
and evaluates them at runtime with eval().
The name 'i' should probably be changed to something short but descriptive.
Here is an interesting idea from a posting by Marcin Kowalczyk:
> It could be nice to have str() working like this instead.
> It's backwards compatible with current 0- and 1-argument cases.
> Except it doesn't work with unicode() which uses positional arguments
> for other things (encoding) :-( Mandating that they are keyword
> arguments would break compatibility.
More information about the Python-list