Compile time evaluation of dictionaries

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Mar 10 19:50:18 EST 2011


On Thu, 10 Mar 2011 16:27:17 -0800, Chris Rebert wrote:


> 3. %-formatting is "obsolete and may go away in future versions of
> Python." (See
> http://docs.python.org/py3k/library/stdtypes.html#old-string-formatting-
operations
> )

There is an awful lot of opposition to that. If it ever happens, it 
probably won't happen until Python4, but even if it happens sooner, you 
could replace

"one:%(one)s two:%(two)s" % \
    {"one": "is the loneliest number", "two": "can be as bad as one"}

with the format string equivalent, still using literals, and the same 
constant-folding optimization could occur.

"one:{one} two:{two}".format(
    **{"one": "is the loneliest number", "two": "can be as bad as one"})

Admittedly, it would require a significantly smarter peephole optimizer 
to recognise this as a constant, which pushes up the complexity for no 
additional gain, but the principle still applies.



-- 
Steven



More information about the Python-list mailing list