
Chris Angelico writes:
I'm -1 on the specific idea, though definitely sympathetic to the broader concept of simplified formatting of strings.
So does everybody. But we've seen many iterations: Perl/shell-style implicit interpolation apparently was right out from the very beginning of Python. The magic print statement was then deprecated in favor of a function. So I suppose it will be very hard to convince the BDFL (and anything implicit would surely need his approval) of anything but a function or an operator. We have the % operator taking a printf-style format string and a tuple of values to interpolate. It's compact and easy to use with position indexes into the tuple for short formats and few values, but is nearly unreadable and not easy to write for long formats with many interpolations, especially if they are repeated.
Python's printf-style formatting has its own warts (mainly because of the cute use of an operator, rather than doing it as a function call),
I think the operator is actually a useful feature, not merely "cute". It directs the focus to the format string, rather than the function call.
and still has the problem of having percent markers with no indication of what they'll be interpolating in.
Not so. We have the more modern (?) % operator that takes a format string with named format sequences and a dictionary. This seems to be close to what the OP wants: val = "readable simple formatting method" print("This is a %(val)s." % locals()) (which actually works at module level as well as within a function). I suppose the OP will claim that an explicit call to locals() is verbose and redundant, but if that really is a problem: def format_with_locals(fmtstr): return fmtstr % locals() (of course with a nice short name, mnemonic to the author). Or for format strings to be used repeatedly with different (global -- the "locals" you want are actually nonlocal relative to a method, so there's no way to get at them AFAICS) values, there's this horrible hack: >>> class autoformat_with_globals(str): ... def __pos__(self): ... return self % globals() ... >>> a = autoformat_with_globals("This is a %(description)s.") >>> description = "autoformatted string" >>> +a 'This is a autoformatted string.' with __neg__ and __invert__ as alternative horrible hacks. We have str.format. I've gotten used to str.format but for most of my uses mapped %-formatting would work fine. We have an older proposal for a more flexible form of templating using the Perl/shell-ish $ operator in format strings. And we have a large number of templating languages from web frameworks (Django, Jinja, etc). None of these seem universally applicable. It's ugly in one sense (TOOWTDI violation), but ISTM that positional % for short interactive use, mapped % for templating where the conventional format operators suffice, and str.format for maximum explicit flexibility in programs, with context-sensitive formatting of new types, is an excellent combination.