[Python-ideas] Unpack of sequences
Alexander Belopolsky
alexander.belopolsky at gmail.com
Wed Aug 29 20:07:41 CEST 2012
On Wed, Aug 29, 2012 at 1:28 PM, Guido van Rossum <guido at python.org> wrote:
> it's probably better not to add a language feature and let users write
> what they want.
How can users write a solution that does not require repetition of
variable names? Of course I can write something like
a, b, c = [m.get(x) for x in ('a', 'b', 'c')]
but when I have more and longer names, this gets tedious.
As far as syntax goes, I also find a, b, c = **m somewhat unintuitive.
I would prefer
{a, b, c} = m
and
{a, b, c, **rest} = m.
> I'd be even less sure about something syntactically similarly plausible like
>
> self.a, self.b, self.c = **foo
I don't think unpacking into attributes is as useful as unpacking into
locals. Object attribute lists are often available programmatically
and it is a simple matter to supply an _update() function that can be
used as self._update(locals()) after values have been assigned to
locals or simply use self._update(m) directly.
(I recall that something like {'a': x, 'b': y} = m has been suggested
and rejected in the past. That syntax also required explicit
specification of the keys to be unpacked.)
More information about the Python-ideas
mailing list