[Python-ideas] lazy tuple unpacking

Paul Tagliamonte paultag at gmail.com
Tue Jul 8 18:17:37 CEST 2014


On Tue, Jul 08, 2014 at 10:30:16AM +0200, Todd wrote:
>    Besides the issues others have discussed, another issue I see here is that
>    you are basically copying the iterator.  In the case of this, where
>    "gen_a" is a generator :
> 
>    >>> a, b, c, *others = gen_a
> 
>    "others" should be the same as "gen_a" in the end (i.e. "others is gen_a
>    == True").  This seems redundant, especially when we have the itertools
>    "take" recipe which can be used to retrieve the first "n" values of an
>    iterator, which can then be unpacked in whatever way you want.
> 
>    However, there might be an alternative.  You could have something where,
>    if you are unpacking an iterable to N variables, you can tell it to just
>    unpack the first N values, and the iterable then remains at position N
>    (similar to "take", but integrated more deeply).  For the case of
>    something like a list or tuple, it will just unpack those variables and
>    skip the rest.  Maybe either a method like this:
> 
>    >>> a, b, c = gen_a.unpack()
> 
>    Or some sort of syntax to say that the remaining values should be skipped
>    (although I don't really know what syntax would be good here, the syntax I
>    am using here is probably not good):
> 
>    >>> a, b, c, [] = gen_a
> 
>    Of course with "take" so simple to implement, this is probably way
>    overkill.  I also don't know if it is even possible for the right side of
>    the expression to know how the layout of the left in that way.

Yeah, I think all the productive ideas (thanks, Andrew and Todd) to make
this happen are mostly starting to converge on full-blown lazy lists,
which is to say, generators which are indexable, sliceable, and work from both
ends (which is to say: more than the current iterator protocol).

I totally like the idea, not sure how keen everyone will be about it.

I'm not sure I have the energy or drive to try and convince everyone on
python-dev this is a good idea, but I'd totally love to play around
with this. Anyone else?

Cheers,
  Paul

-- 
#define sizeof(x) rand()
</paul>
:wq
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140708/6006027c/attachment.sig>


More information about the Python-ideas mailing list