[Python-ideas] Unpacking a dict
mertz at gnosis.cx
Wed May 25 22:16:59 EDT 2016
On Wed, May 25, 2016 at 6:56 PM, Steven D'Aprano <steve at pearwood.info>
> What is your evidence for this claim? So far I've only seen one real-
> world use-case for this, and that single use-case would be well served
> by a simpler syntax:
> a, b, c = **mapping
> which just requires that a, b, c etc are legal names (not general
> identifiers). The dict is then unpacked:
> a = mapping['a']
I can see how this spelling might be intuitive at first brush. But the
more I think about it, the more I recoil against the violation of a
relatively uniform semantic principle in Python.
In no other case in Python, does the RHS of an assignment "probe into" the
LHS to figure out how to determine its value. Moreover, the idea that
variable names are not just bindings, but also pseudo-literals, or maybe
something akin to a Lisp 'symbol', feels enormously unpythonic to me.
Moreover, given that comprehensions are already available, and can express
every variation we might want simply, I see no point of having this mild
syntax sugar. This includes binding in the usual style to arbitrary names,
but also all the expected mechanisms of derived values and conditionals.
You can write:
a, b, c = (mapping[x] for x in ['a','b','c'])
But equally you can write a natural extension like:
x, y, z = (2*mapping[x] for x in get_keys() if x.isupper())
Special casing the very simplest thing to save a minimal number of
characters does not seem worthwhile.
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons. Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas