[Python-ideas] Boundaries for unpacking
Todd
toddrjen at gmail.com
Thu Apr 7 13:44:50 EDT 2016
On Thu, Apr 7, 2016 at 1:03 PM, Michel Desmoulin <desmoulinmichel at gmail.com>
wrote:
> Python is a lot about iteration, and I often have to get values from an
> iterable. For this, unpacking is fantastic:
>
> a, b, c = iterable
>
> One that problem arises is that you don't know when iterable will
> contain 3 items.
>
> In that case, this beautiful code becomes:
>
> iterator = iter(iterable)
> a = next(iterator, "default value")
> b = next(iterator, "default value")
> c = next(iterator, "default value")
>
I think rather than adding a new syntax, it would be better to just make
one of these work:
a, b, c, *d = itertools.chain(iterable, itertools.repeat('default value'))
a, b, c = itertools.chain(iterable, itertools.repeat('default value'))
a, b, c = itertools.chain.from_iterable(iterable, default='default value')
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160407/80f01c10/attachment.html>
More information about the Python-ideas
mailing list