Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 preferred?: multiple assignment
#1
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?
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#2
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).
Quote
#3
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.
Skaperen likes this post
Quote
#4
(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.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#5
(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 :)
Quote
#6
(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
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#7
(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.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Error: int object does not support item assignment ankita_nthu 2 116 Jul-07-2019, 02:14 PM
Last Post: ankita_nthu
  Reproducing assignment with min cost flow in networkx mntfr 0 110 Jun-13-2019, 04:06 PM
Last Post: mntfr
  IndexError: list assignment index out of range Apretext 3 274 Apr-25-2019, 09:29 PM
Last Post: Yoriz
  How do I use this? TypeError: 'NoneType' object does not support item assignment ejected 9 1,307 Mar-26-2019, 05:06 AM
Last Post: ejected
  assignment: not an operator nor expression, but x=y=z=3 works fine? jefdaels 1 249 Jan-29-2019, 02:19 PM
Last Post: perfringo
  How many variables/values are limited to single assignment operator? Prabakaran141 1 407 Sep-06-2018, 03:32 PM
Last Post: Larz60+
  Is Event.set() the preferred way to stop a thread? svetlanarosemond 5 591 Jul-17-2018, 08:14 AM
Last Post: DeaD_EyE
  2-D list element assignment ashutosh759 1 472 May-25-2018, 01:30 AM
Last Post: scidam
  local variable 'option' referenced before assignment RedSkeleton007 11 1,833 Feb-23-2018, 07:16 AM
Last Post: RedSkeleton007
  python nested list assignment weird behavior eyalk1 2 1,063 Jan-16-2018, 07:32 PM
Last Post: wavic

Forum Jump:


Users browsing this thread: 1 Guest(s)