[Python-Dev] Extending tuple unpacking

Ron Adam rrr at ronadam.com
Tue Oct 11 06:13:53 CEST 2005


Delaney, Timothy (Tim) wrote:
> Paul Du Bois wrote:
> 
> 
>>On 10/10/05, Nick Coghlan <ncoghlan at gmail.com> wrote:
>>
>>>   cmd, *args = input.split()
>>
>>These examples also have a reasonable implementation using list.pop(),
>>albeit one that requires more typing.  On the plus side, it does not
>>violate 
>>DRY and is explicit about the error cases.
>>
>>  args = input.split()
>>  try:
>>    cmd = input.pop(0)
>>  except IndexError:
>>    cmd = ''
> 
> 
> I'd say you violated it right there ... (should have been)::
> 
>     args = input.split()
> 
>     try:
>         cmd = arg.pop()
>     except IndexError:
>         cmd = ''
> 
> FWIW, I've been +1 on * unpacking since I first saw the proposal, and
> have yet to see a convincing argument against it other than people
> wanting to stick the * anywhere but at the end. Perhaps I'll take the
> stdlib challenge (unfortunately, I have to travel this weekend, but I'll
> see if I can make time).
> 
> Tim Delaney

I'm +1 for some way to do partial tuple unpacking, yet -1 on using the * 
symbol for that purpose outside of functions calls.

The problem is the '*' means different things depending on where it's 
located.  In a function def, it means to group or to pack, but from the 
calling end it's used to unpack.  I don't expect it to change as it's 
been a part of Python for a long time and as long as it's only used with 
argument passing it's not too difficult to keep straight.

My concern is if it's used outside of functions, then on the left hand 
side of assignments, it will be used to pack, but if used on the right 
hand side it will be to unpack.  And if it becomes as common place as I 
think it will, it will present confusing uses and or situations where 
you may have to think, "oh yeah, it's umm... unpacking here and umm... 
packing there, but multiplying there".  The point is it could be a 
stumbling block, especially for new Python users.  So I think a certain 
amount of caution should be in order on this item.  At least check that 
it's doesn't cause confusing situations.

I really would like some form of easy and efficient tuple unpacking if 
possibly.  I've played around with using '/' and '-' to split and to 
partially unpack lists, but it's probably better to use a named method. 
  That has the benefit of always reading the same.

Also packing tuples (other than in function defs) isn't needed if you 
have a way to do partial unpacking.

     a,b,c = alist[:2]+[alist[2:]]  # a,b,rest

Not the most efficient way I think, but maybe as a sequence method 
written in C it could be better?

Cheers,
    Ron














More information about the Python-Dev mailing list