[Python-ideas] Unpack of sequences

Guido van Rossum guido at python.org
Wed Aug 29 19:28:12 CEST 2012


On Wed, Aug 29, 2012 at 10:18 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> On 30/08/12 03:01, Steven D'Aprano wrote:
>>> a, b, x, y = **mapping
>>>> could be equivalent to:
>>>> a, b, x, y = mapping['a'], mapping['b'], mapping['x'], mapping['y']
>>>> Oh, I forgot to mention... extra keys are ignored. That's because in
> my experience, you are likely to have a mapping with many different keys
> (say, a set of config options in a dict) and you only want to extract a
> few at a time. Unlike unpacking a tuple, you're unlikely to need *every*
> field at once.
>>> A potentially useful extension to the idea is to capture the extra
> items in a dict:
>> a, b, x, y, **extras = **mapping
>> which is like:
>> a, b, x, y = [mapping[name] for name in ('a', 'b', 'x', 'y')]
> extras = dict((k, v) for k,v in mapping.items() if k not in ('a', 'b', 'x', 'y'))

Sounds to me like there are so many potential variations here that
it's probably better not to add a language feature and let users write
what they want. (My personal variation would be to use m.get(k)
instead of m[k].)
I think if I encountered the line
a, b, c = **foo
I would have only the vaguest intuition for its meaning -- and I'd be
even less sure about something syntactically similarly plausible like
self.a, self.b, self.c = **foo
-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-ideas mailing list

AltStyle によって変換されたページ (->オリジナル) /