Multiple assignment semantics?

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 "", line 1, in 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').

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 "", line 1, in 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').

1 Upvote for showing a great use case I'd never thought of! – kindall Mar 3 at 15:34.

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.

For me this behavior seems to violate (at least): There should be one-- and preferably only one --obvious way to do it. And Special cases aren't special enough to break the rules. Thanks – eat Mar 3 at 15:44 @eat: I definitely would not call this a bug.

It is clearly documented. I'm just wondering about the rationale for this design decision. – Sven Marnach Mar 3 at 15:51 OK, not a bug, but when you say that just wondering about the rationale for this design decision makes me also curious and perplexed of what would be a (even single) justifiable use case for ...= then?.

Thanks – eat Mar 3 at 16:22.

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.

– pessimopoppotamus Mar 3 at 15:19 In the case, When the LHS is a single identifier, then there is only one STORE_FAST call is made. Otherwise it is all same. – Senthil Kumaran Mar 3 at 15:50.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions