3 ms·
Does this work for you? key, *value = item.split("=", 1) if value: value = value[0] else: value = None
by klyrs 5y ago
Does this work for you?
key, *value = item.split("=", 1)
if value:
value = value[0]
else:
value = None
- woodruffw 5y agoYep, that's a pattern I've applied before. It's not the worst, but I don't think it has the clarity that this would have: 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.)
- xtreak29 5y agoThere was a similar PEP https://peps.python.org/pep-0505/ https://peps.python.org/pep-0505/
- srfwx 5y agovalue = value[0] if value else None