[Python-ideas] lazy tuple unpacking
Andrew Barnert
abarnert at yahoo.com
Sat Jul 5 13:26:26 CEST 2014
On Friday, July 4, 2014 6:11 PM, Chris Rebert <pyideas at rebertia.com> wrote:
> On Fri, Jul 4, 2014 at 5:59 PM, Paul Tagliamonte <paultag at gmail.com>
> wrote:
> <snip>
>> I notice that:
>>
>> >>> a, b, c, *others = g_range(100)
>>
>> Works great. Super useful stuff there. Looks good.
>>
>> I also notice that this causes *others to consume the generator
>> in a greedy way.
>>
>> >>> type(others)
>> <class 'list'>
>>
>> And this makes me sad.
>>
>> >>> a, b, c, *others = g_range(10000000000)
>> # will also make your machine very sad. Eventually resulting
>> # (ok, unless you've got a really fancy bit of kit) in:
>> Killed
>>
>> Really, the behavior (I think) should be more similar to:
>>
>> >>> _x = g_range(1000000000000)
>> >>> a = next(_x)
>> >>> b = next(_x)
>> >>> c = next(_x)
>> >>> others = _x
>> >>>
>>
>>
>> Of course, this leads to all sorts of fun errors, like the fact you
>> couldn't iterate over it twice. This might not be expected. However, it
>> might be nice to have this behavior when you're unpacking a generator.
>>
>> Thoughts?
>
> It would mean an (IMHO undesirable) loss of consistency/symmetry,
> type-wise, with other unpackings where this generator optimization
> isn't possible:
>
> Python 3.4.1 (default, May 19 2014, 13:10:29)
>>>> x = [1,2,3,4,5,6,7]
>>>> a,b,*c,d = x
>>>> c
> [3, 4, 5, 6]
>>>> *e,f,g = x
>>>> e
> [1, 2, 3, 4, 5]
When I was experimenting with adding lazy lists (sequences that wrap an iterator and request each value on first request), I played around with PyPy (Python 2.x, not 3.x), making unpacking (as well as map, filter, etc.) return them instead of iterators. (I also tried to make generator functions and expressions return them, but I couldn't get that to work in a quick hack…)
It worked nicely, and I think it would work with the expanded unpacking in 3.x. In Paul's original example, others is a lazy list of 9999999997 elements, which will only evaluate the ones you actually ask for. In Chris's examples, c and e are fully-evaluated lazy lists of 4 or 3 elements, respectively.
But, even if this weren't a ridiculously radical change to the language, I don't think it's what you'd want.
First, an iterator over 9999999997 elements is a lot more useful than a lazy list of 9999999997 elements, because you can iterate the whole thing without running out of memory.
Second, it wouldn't help in a case like this:
a, b, *c, d, e = range(10000000000)
To make that work, you'd need something smarter than just using a lazy list instead of an iterator.
One way to solve it is to try to keep the original type, so unpacking maps to something like this:
try:
a, b, c, d, e = i[0], i[1], i[2:-2], i[-2], i[-1]
except TypeError:
# current behavior
Then c ends up as range(2, 9999999998), which is the best possible thing you could get there.
You could take this even further by adding either a notion of bidirectional and forward-only sequences, or a notion of reversible iterables, but that's getting much farther into left field; if anyone's interested, see http://stupidpythonideas.blogspot.com/2014/07/lazy-tuple-unpacking.html for details.
More information about the Python-ideas
mailing list