[Python-ideas] Unpack of sequences

Cesare Di Mauro cesare.di.mauro at gmail.com
Wed Aug 29 18:56:58 CEST 2012


2012/8/29 M.-A. Lemburg <mal at egenix.com>

> Cesare Di Mauro wrote:
> > 2012/8/29 Mike Graham <mikegraham at gmail.com>
> >
> >> On Wed, Aug 29, 2012 at 11:45 AM, Masklinn <masklinn at masklinn.net>
> wrote:
> >>> On 2012-08-29, at 17:03 , Guido van Rossum wrote:
> >>>>
> >>>> Also of course assignment has no equivalent to keyword parameters
> >>>
> >>> I've always thought it would be a rather neat way to unpack
> >>> dictionaries, instead of doing it by hand or abusing `itemgetter` to
> get
> >>> values in a known order.
> >>
> >> Do you have a suggestion of a nice syntax for a thing to unpack
> >> mappings (or to unpack things by attributes)?
> >>
> >> Mike
> >>
> >
> > a: x, b: y, c: z = {'a': 'x', 'b': 'y', 'c': 'z'}
>
>

Would this assign 'a' to a or just use a as key for the lookup ?
> If the former, where would you take the lookup order from ?
> If the latter, what about keys that are not valid Python identifiers ?
>
>
The latter. But we already have problems with Python identifiers:

>>> def f(**Keywords): pass
>>> f(**{1 : 'BAD!'})
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
f(**{1 : 'BAD!'})
TypeError: f() keywords must be strings

It wasn't a feature blocker...

mxTools has a function extract() to extract values from a mapping
> or sequence object:
>
> extract(object,indices[,defaults])
>   Builds a list with entries object[index] for each index in the sequence
> indices.
>   (see http://www.egenix.com/products/python/mxBase/mxTools/doc/)
>
> >>> mx.Tools.extract(d, ('a', 'b', 'c'))
> ['x', 'y', 'z']
>
> IMO, that's a much cleaner way to express what you'd like Python
> to do.
>
> --
>  Marc-Andre Lemburg
>

Yes, may be an extract method for mappings will be finer.

Cesare
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120829/1c3a8d4b/attachment.html>


More information about the Python-ideas mailing list