[Python-ideas] Custom converters in str.format() and f-strings

Steven D'Aprano steve at pearwood.info
Sat May 4 14:09:59 EDT 2019


On Sat, May 04, 2019 at 06:51:39PM +0300, Serhiy Storchaka wrote:

> It is less important for f-strings because you can use arbitrary 
> expressions, but even in this case f"Hello, {name!x}!" or f"Hello, 
> {name!xml}!" looks better than f"Hello, {html.escape(name)}!" or 
> f"Hello, {x(name)}!".

html.escape(name) is mostly self-documenting. !x is a cryptic, arbitrary 
code. Why !x rather than !e ("escape") or !h ("html") or !g for that 
matter?

Compact mini-languages like regexes and format codes are compact but 
cryptic. They're harder to read, especially when you're reading less 
common codes. Probably most people could guess that %y in a date format 
string means "year", but how many people will recognise %G and %j 
without looking them up?



-- 
Steven


More information about the Python-ideas mailing list