Python has a long history of overloading its operators. I observe that today’s Python ships with several different distinct meanings for the |
operator: “boolean or”, “union of sets”, and the recently-added “create union of types”. Also, pathlib.Path
overloads the /
operator to mean “smart-concatenate elements of a path”, a wildly different meaning from division–and %
has meant “perform value substitution inside a string” for decades. So I suggest that Python programmers are mentally flexible enough to understand context-specific meanings for operators.
Also, |
has meant “pipe things together” for 40+ years in the UNIX and DOS shells. Again, this spelling was so obvious to the developers of three separate template libraries that they all used it. Each of these libraries could have spelled apply-filter as
filter(expression)
but felt it was conceptually important enough for some reason to use the spelling
expression | filter
On the other hand, it’s worth noting the ticklish problem of using an existing legal operator to mean “filter” here. What if the user innocently wants to evaluate this expression in a template?
set1 | set2
The template might interpret that to mean “evaluate set1
, then run it through the filter set2
”, oops!
Obviously picking a different operator solves this problem. But I feel like we’re running out of ASCII punctuation that looks nice and would be unambiguous here.
I observe you could solve this problem a couple other ways. For example, you could declare that you can only specify filters after the colon, and if you don’t need a format-spec just leave it blank:
my_value :| filter1 | filter 2
Alternatively, if you want the filter syntax to take precedence inside template strings, users could use a normal |
operator by putting parentheses around the expression:
(set1 | set2)
I fear I have no more to offer this conversation. As mentioned I’m not really an expert in this area. Maybe y’all could rope in some sort of domain expert, Armin Ronacher or somebody.
[Edit: oops, you concatenate pathlib.Path
elements with the /
operator, not the |
operator. D’oh!]