by woodruffw · 4y ago · 0 comments
# oops, ValueError on `item = "noequals"`
key, value = item.split("=", 1)
That alone makes destructuring somewhat limited, unless I'm either willing to validate my lengths ahead of time or do exception-driven control flow. Something like `?=` perhaps, to complement the walrus operator. key, sep, value = item.partition("=")
If you need to know whether there was a split/match that occurred you could test sep's truthiness. key, *value = item.split("=", 1)
if value:
value = value[0]
else:
value = None key, value ?= item.split("=", 1)
Modulo bike shedding over the actual syntax, of course.(Edit: the semantics of this could be even more generic, like Ruby's `[...] rescue value` syntax for turning any raised exception into a default value instead.)
value = value[0] if value else None