1

I am new to Python and I am trying to understand this syntax:

a, b = b, a + b
skeletank
2,9165 gold badges44 silver badges82 bronze badges
asked Aug 25, 2011 at 21:10
5
  • 1
    It's called parallel assignment. Commented Aug 25, 2011 at 21:13
  • @miku: Neato - I've used the technique many times, but didn't realize there was a formal term for it. Commented Aug 25, 2011 at 21:19
  • lol. This is like an interview question. I kind of hope nobody actually writes the above code, but then, I think, probably I would. It would make more sense with real variable names at least. Commented Aug 26, 2011 at 0:35
  • @morningstar looks like a perfectly normal line of code to write, in a recursive fibonacci number function Commented Aug 26, 2011 at 2:20
  • This is called multiple assignment... it sets a to b, and b to a+b Commented Mar 23, 2022 at 20:32

7 Answers 7

2

We could rewrite this to: (a, b) = (b, a + b)

Considering that a = 3 and b = 6

The operation (b, a + b) returns a tupple (6, 9) and assigns these values to the listed variables (a, b) and assign (a = 6, b = 9).

So the final values are a = 6 and b = 9.

answered Aug 25, 2011 at 21:15
Sign up to request clarification or add additional context in comments.

Comments

1

This is syntactic sugar for a python feature called 'unpacking'. It effectively means this:

(a, b) = (b, a + b) # This is also valid syntax

The tuple (b, a + b) is created, locking in the values. Then, the values are piece-wise assigned to the identifiers in the tuple (a, b). Since the values are locked in before assignment starts, each takes on the expected value. This idea is derived from pattern matching in Haskell.

answered Aug 25, 2011 at 21:16

Comments

1

This is called sequence unpacking. The right-hand side is packed into a tuple (because of the comma). Python packs then evaluates the right side, then unpacks those values into the left hand side.

See Tuples and Sequences.

answered Aug 25, 2011 at 21:17

Comments

1

a is given the value of b and b is given the value of a+b

A more technical explanation can be found here

answered Aug 25, 2011 at 21:12

Comments

1

Python has the ability to transfer multiple values at once. That means "set a to b, and b to the sum of a and b".

There is a more comprehensive explanation here.

answered Aug 25, 2011 at 21:12

2 Comments

The link is dead, did you mean different page maybe?
@ShadowWizard Nope. I meant that when I wrote it. Unfortunately, the link has since moved, but you can still find it with the wayback machine: web.archive.org/web/20100419171004/http://diveintopython.org/…
0

It is an assignment with a tuple-decomposition (or sequence unpacking) and might also be called a multiple assignment statement.

The tuple on the RIGHT is evaluated and then the result is assigned slot-for-slot to the variables on the LEFT. (The left side is not really a tuple even though the syntax makes it look like one.)

So,

a = 1
b = 2
a, b = b, a + b
// a, b = 2, 1 + 2 
// a, b = 2, 3
// a = 2
// b = 3

Happy coding.

answered Aug 25, 2011 at 21:15

Comments

0

it does two initializations in one line of code:

1. a = b
2. b = a + b

it's a alternative to use it as a swap() function in python by the way.

answered Dec 20, 2018 at 11:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.