On Wed, Oct 21, 2020 at 1:13 AM Rob Cliffe email@example.com wrote:
On 20/10/2020 13:05, Chris Angelico wrote:
On Tue, Oct 20, 2020 at 10:37 PM Rob Cliffe via Python-ideas firstname.lastname@example.org wrote:
In short, "assigning" to f-strings is not and cannot be a simple reversal of having them in expressions. Rather, it is opening a big can of worms.
It's not a reversal of them being in expressions any more than assigning to a list display is a reversal of constructing a list. It's a parallel operation, a counterpart.
It would be Python's way of offering an sscanf-like operation, which is something that I've frequently wished for. Regular expressions aren't ideal for all situations, and there are plenty of times when a simple set of parsing rules could be very nicely encoded into a compact form like this. C's sscanf and sprintf aren't perfect counterparts, but they're incredibly valuable. Python has percent formatting for sprintf, but no form of sscanf.
Er, well, why not just add a sscanf to Python?
A couple of reasons, but the main one is that you can't have "output variables" (in C, sscanf takes pointers, so you pass it the address of your variables), which means that all you'd get is a fancy string split and then you put all the assignment on the left. That leads to an all-or-nothing result for the simple form, and no easy way to do anything else. Consider:
a, b, c = sscanf("%d %d %d", "123 432")
Boom! Instead of having a clean way to represent partial parsing.
It'd still be better than regex partial parsing, where you get absolute silence and no way to debug it, but it's not nearly as good as could be done.