multiple assignment

A

Anand

Suppose i have a big list and i want to take tke the first one and rest
of the list like car/cdr in lisp.
is there any easy way to do this in python?

Only way i know is

a = range(10)
x, y = a[0], a[1:]

In perl, it is possible to do multiple assignment like this....

@a = (1, 2, 3);
($x, @y) = @a;

I find this is a good feature and missing in python.
Why can't python support something like this:

x, *y = a

* operator is used in the usual sence here.

This is not really a new concept to python, infact when calling a
function which takes variable arguments, it is used in a similar way.
for example:

def f(x, *y): print x, y
f(1, 2, 3)

the argument passing here is very similar to the multiple assignment x,
*y = (1, 2, 3)

any comments?

-anand
 
C

Christoph Zwerschke

Anand said:
Suppose i have a big list and i want to take tke the first one and rest
of the list like car/cdr in lisp.
is there any easy way to do this in python?

Only way i know is

a = range(10)
x, y = a[0], a[1:]

You have so many higher-level ways to access and iterate through lists
in Python, that you normally just don't need to do things like that.

Also, in the frequent case where y=a, you can just write x = a.pop(0).
> Why can't python support something like this:
>
> x, *y = a
>
> This is not really a new concept to python, infact when calling a
> function which takes variable arguments, it is used in a similar way.

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.)

But I don't know if this is really useful enough...

-- Christoph
 
F

Fredrik Lundh

Christoph said:
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.)

But I don't know if this is really useful enough...

things like this are proposed from time to time (I haven't looked, but
there might even be a PEP somewhere). however, function calls and
assignments are two different things, and I'm not convinced that it's
not yet another hypergeneralization...

</F>
 
A

Anand

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
 
A

Anand

Wouldn't it be nice to say
id, tokens_str = line.split(',', 1)

But then you have to split tokens_str again.

id, tokens_str = line.split(',', 1)
tokens = tokens_str.split(',')

this is too verbose.

anand
 
S

Steven Bethard

Anand said:
But then you have to split tokens_str again.

id, tokens_str = line.split(',', 1)
tokens = tokens_str.split(',')

Sorry, it wasn't clear that you needed the tokens from the original post.

If you're interested in this, the best route is to write up a PEP and
provide an implementation. The new AST in Python makes this kind of
thing a bit easier.

Steve
 
M

Magnus Lycka

Anand said:
Suppose i have a big list and i want to take tke the first one and rest
of the list like car/cdr in lisp.
is there any easy way to do this in python?

It seems like overkill to me to make the syntax more
complex just to avoid writing a one-line function.

def first_rest(seq): return seq[0], seq[1:]
 
B

bruno at modulix

Anand said:
But then you have to split tokens_str again.

id, tokens_str = line.split(',', 1)
tokens = tokens_str.split(',')

this is too verbose.

head_tail = lambda seq: seq[0], seq[1:]

....
id, tokens = head_tail(line.split(','))


but I do like the syntax you're proposing !-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,453
Members
48,137
Latest member
IndiraMcCo

Latest Threads

Top