[Python-Dev] Half-baked proposal: * (and **?) in assignments

Nathan Clegg nathan-nntp@geerbox.com
Sat, 23 Nov 2002 09:52:40 -0800


Brian Quinlan wrote:

> year, month, day = time.localtime()[0:3]
> 
> or 
> 
> year, month, day, *dummy = time.localtime()

I think Gareth's split example is better.  localtime returns a
fixed-length tuple.  Things are much more interesting when you don't
know how many elements you're getting.  Which do you like better for,
say, parsing required and optional parameters to a command:

(cmd, req1, req2, req3, opt_str) = input.split(None, 4)
opts = opt_str.split()
execute_cmd(cmd, req1, req2, req3, opts)

or

parts = input.split()
(cmd, req1, req2, req3) = parts[:4]
opts = parts[4:]
execute_cmd(cmd, req1, req2, req3, opts)

or

(cmd, req1, req2, req3, *opts) = input.split()
execute_cmd(cmd, req1, req2, req3, opts)


A really interesting possibility is:

(first, *middle, last) = input.split()

This breaks the parallel to parameter passing, but would be very
handy.

Are we getting too perlish?



-- 
Nathan Clegg
GeerBox
nathan-nntp@geerbox.com