multiple assignment
Anand
anand.chittu at gmail.com
Wed Mar 22 10:00:28 EST 2006
> You're right, that would not be so far off.
> But then, the following should be also supported:
>
> *x, y = a # x, y = a[:-1], y = a[-1]
> x, *y, z = a # x, y, z = a[0], a[1:-1], a[-1]
>
> Of course, there can be only one variable with an asterisk.
> (But note that in the situation of a function taking parameters, that
> variable must always be the last.)
Same argument can be applied for functions also. whats wrong in having
some thing like this?
def f(x, *y, z): pass
I think there is a problem in both these cases.
> But I don't know if this is really useful enough...
I think it is really useful.
One which i encountered was there is a file where each line has tokens
separated by commas. First token is the id and i want to use it in a
special way.
Wouldn't it be nice to say
id, *tokens = line.split(',')
than
tokens = line.split(',')
id = tokens.pop(0)
- anand
More information about the Python-list
mailing list