
On Wed, Oct 21, 2020 at 3:46 AM Steven D'Aprano steve@pearwood.info wrote:
On Wed, Oct 21, 2020 at 03:19:20AM +1100, Chris Angelico wrote:
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.
Then it won't go "Boom!" as you said. It will just return the default.
So your boom objection is neutralised, yay!
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 :)
You still have to test for None. Perhaps not as awkward as try...except, but you still have to test each one.
Often you don't have to test for it. It's not uncommon for the default to be a real value (eg 0 when parsing for numbers - lots of file formats will work that way), so there's no testing to be done. Or you can just do "cheese or X" to handle (a) unassigned and (b) blank, which again is a common situation.
ChrisA