
On Wed, Oct 21, 2020 at 3:04 AM Steven D'Aprano <steve@pearwood.info> wrote:
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.
Okay, let's try "partial parsing" with an f-string style:
f"{spam:d} {eggs:d} {cheese:d}" = "123 456"
Now what? How do you use this in practice?
In every sscanf-like system I've used, there is a default value of some sort, either because variables are automatically initialized, or because the sscanf construct itself provides a default. You can always explicitly initialize them if you need to: spam = eggs = cheese = None f"{spam:d} {eggs:d} {cheese:d}" = "123 456" Oh look, not nearly as ugly as your strawman :)
I think having "Boom!" (an exception) when your pattern doesn't match is the right thing to do, at least by default. But if you want to get fancy, we can supply a missing value:
spam, eggs, cheese = sscanf("%d %d %d", "123 432", missing=None) assert spam == 123 assert eggs == 432 assert cheese is None
Sure, if that's what you want. The other thing that frequently comes up in my code, though, is something where the earlier matches indicate whether something else is needed. In that situation, the earlier entries - which would have been assigned to - control whether you even look at the later ones. There's no issues that way either. I don't think this is nearly as big a concern as you think, and it's way WAY easier to work with than a regex. Just because you don't want the partial assignment feature doesn't mean it isn't incredibly practical and useful in real-world situations :) ChrsiA