Sorry, I accidentally sent before I was done. Tushar Sadhwani writes:
Since Python has built-in syntax for interpolated strings, I believe it's a good area to idea to extend it to pattern matching, like so:
def unquote(string: str) -> str: match string: case f'"{value}"': return value case f"'{value}'": return value case _: return string
I think this is a pretty unconvincing example. While people seem to love to hate on regular expressions, it's hard to see how that beats def unquote(string: str) -> str: m = re.match(r"""^(?: "(.*)" # "-delimiters |'(.*)' # '-delimiters |(.*)) # no delimiters $""", string, re.VERBOSE) return m.group(1) or m.group(2) or m.group(3) and this is absolutely clearer than either pattern-matching approach: def unquote(string: str) -> str: # Gilding the lily, but it's obvious how to extend to other # symmetric delimiters, and straightforward for asymmetric # delimiters. for quotechar in ("'", '"'): if string.startswith(quotechar) and string.endswith(quotechar): return string[1:-1] else: return string
Doing this with current match syntax is not as easy.
Sure, but that's why we have .startswith and .endswith, and for more complex cases, why we have regular expressions. Chris Angelico has proposed adding (something like) C's scanf to the stdlib, as well.
I have other reasons to consider this idea as well,
Given the above, I really think you need to bring those up if you want to pursue this idea.
but before I try to pursue this, I'd like to know if something like this was already discussed when the proposal was being made?
Looking at PEPs 634-6, and especially 635, I doubt it (but I didn't participate in those discussions). On the one hand, the match statement is for matching (a subset of) Python expression structure, not string structure. On the other, although an f-string *is* an expression, it doesn't really feel like one, to me, anyway. Also, I think it would be miserable to code (do you really want to support the full replacement field syntax with widths, justification, precision, and more) and the bikeshedding will be horrific (supposing you support a restricted field syntax with no width and precision, is case f"{value:03.2f}": an error or do you ignore the padding, width, and precision?) It's a very interesting suggestion, but I suspect the bikeshedding is going to be more fun than the implementation. Steve