Multiple assignment semantics

PythonVariable Assignment

Python Problem Overview


In Python one can do:

a, b   = 1, 2
(a, b) = 1, 2
[a, b] = 1, 2

I checked the generated bytecode using dis and they are identical.
So why allow this at all? Would I ever need one of these instead of the others?

Python Solutions


Solution 1 - Python

One case when you need to include more structure on the left hand side of the assignment is when you're asking Python unpack a slightly more complicated sequence. E.g.:

# Works
>>> a, (b, c) = [1, [2, 3]]

# Does not work
>>> a, b, c = [1, [2, 3]]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 2 values to unpack

This has proved useful for me in the past, for example, when using enumerate to iterate over a sequence of 2-tuples. Something like:

>>> d = { 'a': 'x', 'b': 'y', 'c': 'z' }
>>> for i, (key, value) in enumerate(d.iteritems()):
...     print (i, key, value)
(0, 'a', 'x')
(1, 'c', 'z')
(2, 'b', 'y')

Solution 2 - Python

Python tuples can often be written with or without the parentheses:

a = 1, 2, 3

is equivalent to

a = (1, 2, 3)

In some cases, you need parentheses to resolve ambiguities, for examples if you want to pass the tuple (1, 2) to the function f, you will have to write f((1, 2)). Because the parentheses are sometimes needed, they are always allowed for consistency, just like you can always write (a + b) instead of a + b.

If you want to unpack a nested sequence, you also need parentheses:

a, (b, c) = 1, (2, 3)

There does not seem to be a reason to also allow square brackets, and people rarely do.

Solution 3 - Python

When unpacking a single-element iterable, the list syntax is prettier:

a,=f()    # comma looks out of place
(a,)=f()  # still odd
[a]=f()   # looks like every other list

Solution 4 - Python

They are also same because the assignment happens from Right to left and on the right, you have one type, which is a sequence of two elements. When the asignment call is made, the sequence is unpacked and looked for corresponding elements to match and given to those values. Yes, any one way should be fine in this case where the sequence is unpacked to respective elements.

Solution 5 - Python

An open parenthesis allows for a multi-line assignment. For example, when reading a row from csv.reader(), it makes code more readable (if less efficient) to load the list into named variables with a single assignment.

Starting with a parenthesis avoids long or \ escaped lines.

(a, b,
c) = [1, 2, 3]

(Imagine more and longer variable names)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAillynView Question on Stackoverflow
Solution 1 - PythonWill McCutchenView Answer on Stackoverflow
Solution 2 - PythonSven MarnachView Answer on Stackoverflow
Solution 3 - PythonDavis HerringView Answer on Stackoverflow
Solution 4 - PythonSenthil KumaranView Answer on Stackoverflow
Solution 5 - PythonverbamourView Answer on Stackoverflow