When I discovered that python has parallel assignment I thought is pretty cool.

Recently I discovered parallel assignment works also in ruby.

For people that don't know it: x,y = y,x in ruby, python this swaps x and y values .

Which language came first with parallel assignment, python? or was it other language?

  • 2
    This is called tuple unpacking in python, not parallel assignment. – Martijn Pieters Sep 6 '12 at 16:19
  • @MartijnPieters Because they are separated by commas python treats x,y like being tuple? – Eduard Florinescu Sep 6 '12 at 16:28
  • I just discovered that in FireBug console [x,y]=[y,x] works too. – Eduard Florinescu Sep 6 '12 at 16:36
  • Exactly; a tuple on the right-hand side will be unpacked into a series of variables on the left, if the lengths match. The y, x value on the right is a tuple because of the comma. – Martijn Pieters Sep 6 '12 at 16:43
  • PHP likewise has the more-verbose list($a, $b) = array(1, 2) – Izkata Sep 6 '12 at 16:56

CPL introduced the feature in 1963, calling it simultaneous assignment. From D.W. Barron et al., "The main features of CPL" (page 140):

24. Simultaneous assignment commands

The general form of an assignment command can now be given. Normally this is an expression yielding an LH value, followed by :=, followed by an expression yielding an RH value. However, if an explicit list is written on the left-hand side then the right-hand side is either an explicit list, or a list expression; in the latter case the transfer function Members is automatically invoked. In this form the two explicit lists must contain the same number of members, and the command denotes a simultaneous assignment of each right-hand member to the corresponding left-hand member. Thus, if L is a list variable and a, b, c are real variables,

L := a, b, c

is an assignment to the list variable, while

a, b, c := L

a, b := b,a

are simultaneous assignments.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.