Instead of explaining why the developer needs to make this change, I would instead start by offering him a superior option. As others on this thread have suggested, introduce a str.sprintf(*args, **kwargs)
method. Since that method is more explicit, it doesn’t need to do as much type checking on the inputs as the str-%
operator. There will be a small, but measurable performance improvement across all Python implementations. We might also be able to show some improvement for another domain such as static analysis. For "%s %s" % a
, a lint tool needs to do more work figuring out whether a
is an int
, tuple
of str
, etc. Release a tool along with this new method to help users auto-convert their existing str-%
usages to str.sprintf
. It’s probably possible for a tool to auto-update at least 90% of these.
The next steps, including whether to deprecate the operator would depend on additional research. Maybe we can find a path that avoids deprecation. Maybe not. Be prepared for any changes to take years, which is perfectly fine and good. I was using the term “moral” jokingly. Large systems are governed by economics.
I realize that my original problem statement (enabling "foo" % x
usage) isn’t exactly compelling people to storm the proverbial gates. In defense of that use case, I think Python is an excellent language for creating metalanguages. The larger project I’m working on is a hardware description language (HDL) embedded in Python: a meta-HDL. I have found using strings as literals to be extremely convenient. For example, here is one module from a RISCV simulation: seqlogic/tests/riscv/core/data_mem_if.py at main · cjdrake/seqlogic · GitHub.
Here’s an example of using literals on the left side of the expression:
self.expr(
bus_wr_be,
Mux(
GetItem(data_format, slice(0, 2)), # might change this to data_format[0:2]
x0=("4b0001" << byte_addr),
x1=("4b0011" << byte_addr),
x2=("4b1111" << byte_addr),
x3="4b0000",
),
)
If you’ve written Verilog before, this will look familiar and easy to adopt.
Apologies for advertising my yak shaving exercise, but I want to point out that some novelties can lead to interesting outcomes if you’re willing to clear a path.
Is this interesting enough to ask the community to expend a couple million man-hours of energy? Probably not. But it’s fun to talk about, and you never know until you ask.