Python Forum
preferred?: multiple assignment - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: preferred?: multiple assignment (/thread-1092.html)



preferred?: multiple assignment - Skaperen - Dec-03-2016

which would be preferred for assigning x to 6 variables:

a,b,c,d,e,f=x,x,x,x,x
vs.

a=b=c=d=e=f=x
and is there also a preferred style?


RE: preferred?: multiple assignment - micseydel - Dec-03-2016

The latter is definitely more concise and to-the-point. I would go with that unless you want identity to different when equality would still be equal (e.g. a bunch of empty tuples).


RE: preferred?: multiple assignment - nilamo - Dec-05-2016

If they're all the same value, I'd prefer the chaining (second example). But I think the first just shouldn't be used for more than two variables, otherwise it gets difficult to read.


RE: preferred?: multiple assignment - Skaperen - Dec-17-2016

(Dec-03-2016, 03:18 AM)Skaperen Wrote: which would be preferred for assigning x to 6 variables:

a,b,c,d,e,f=x,x,x,x,x
vs.

a=b=c=d=e=f=x
and is there also a preferred style?

i guess no one noticed my typo of only 5 xs in the first code.


RE: preferred?: multiple assignment - micseydel - Dec-17-2016

(Dec-17-2016, 03:28 AM)Skaperen Wrote: i guess no one noticed my typo of only 5 xs in the first code.
All the more reason it shouldn't be preferred :)


RE: preferred?: multiple assignment - Skaperen - Dec-19-2016

(Dec-17-2016, 07:56 AM)micseydel Wrote:
(Dec-17-2016, 03:28 AM)Skaperen Wrote: i guess no one noticed my typo of only 5 xs in the first code.
All the more reason it shouldn't be preferred  :)
agreed Dance


RE: preferred?: multiple assignment - Ofnuts - Dec-19-2016

(Dec-19-2016, 08:00 AM)Skaperen Wrote:
(Dec-17-2016, 07:56 AM)micseydel Wrote: All the more reason it shouldn't be preferred  :)
agreed Dance

On the contrary... both of these are invalid Python
a,b,c,e,f=x,x,x,x,x
a,b,c,d,e,f=x,x,x,x
So even if a human doesn't notice the problem, Python will. While:
a,b,d,e,f=x
is correct and you'll be told about the unitialized c much later.