* for generic unpacking and not just for arguments?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Mon Nov 30 15:50:39 CET 2009

On Sun, 29 Nov 2009 13:45:18 -0600, Tim Chase wrote:

>> The feature is available in Python 3.x:
>>>>> a, b, *c = 1, 2, 3, 4, 5
>>>>> a, b, c
>> (1, 2, [3, 4, 5])
>>>>> a, *b, c = 1, 2, 3, 4, 5
>>>>> a, b, c
>> (1, [2, 3, 4], 5)
> This is a nice feature of 3.x but I'm disappointed (especially in light
> of the move to make more things iterators/generators), that the first
> form unpacks and returns a list instead returning the unexhausted
> generator.

So you want *x behave radically different in subtly different contexts? 

a, *x, b = iterator

would give x a list and iterator run to exhaustion, whereas:

a, b, *x = iterator

would give x identical to iterator, which hasn't run to exhaustion, while 
both of these:

a, *x, b = sequence
a, b, *x = sequence

would give x a list, but in the second case, unlike the case of 
iterators, x would not be identical to sequence.

No thank you, I'd prefer a nice, consistent behaviour than a magical "Do 
what I mean" function.

However, having some syntax giving the behaviour you want would be nice. 
Something like this perhaps?

a, b, c = *iterable

equivalent to:

_t = iter(iterable)
a = next(_t)
b = next(_t)
c = next(_t)  # and so on for each of the left-hand names
del _t

That would be useful.


More information about the Python-list mailing list