PEP 750: Tag Strings For Writing Domain-Specific Languages

Nice work!

My main concern with this idea is filling up the namespace with a variety of short names. It’s clearly convenient to have xml or html as a prefix,[1] but I’m not convinced it’s significantly better than a function.

And of course, we can’t use dots in these names, which means we’re doing a function lookup on a top-level function with a short name that could very easily be overwritten at some point.

I’m inclined towards a generic tag that converts the interpolations into structured data. Something like i"str {x:format}" becoming Interpolated(s="str {0}", args=[(x, "format"), ...]).

Then the tags can be regular functions that handle the Interpolated type - xml.parse(i"<e attr={x} />"). It’s not quite as syntactic sugar, but it’s more extensible and safer to use.

And maybe there’s an additional layer of sugar that can be added on top, for rarer cases where you can live with the namespace pollution? But if the only option is to create top-level namespace pollution in order to use it at all, I don’t think this will move as quickly as you’d like.


  1. greet is a poorly motivated example, IMHO. You should lead with something that’s useful. ↩︎

25 Likes