Alternatives¶
ScopeFormatter provides variable interpolation into strings. It is amazingly compact and elegant. Sadly, it only interpolates Python names, not full expressions.
sayhas full expressions, as well as a framework for higher-level printing features beyondScopeFormatter’s…um…scope.interpolate is similar to
say.fmt(), in that it can interpolate complex Python expressions, not just names. Itsi % "format string"syntax is a little odd, however, in the way that it re-purposes Python’s earlier"C format string" % (values)style%operator. It also depends on the nativeprintstatement or function, which doesn’t help bridge Python 2 and 3.Even simpler are invocations of
%orformat()usinglocals(). E.g.:name = "Joe" print "Hello, %(name)!" % locals() # or print "Hello, {name}!".format(**locals())
Unfortunately this has even more limitations than
ScopeFormatter: it only supports local variables, not globals or expressions. And the interpolation code seems gratuitous. Simpler:say("Hello, {name}!")
In the future, PEP 498 may provided some of the auto-formatting of literal strings that
saydoes now.