epeleg
epeleg

Reputation: 10915

multi assignment vs multiple assignments use cases in python

if I want to assign 2 values, would it be considered more pythonic to assign:

speed, acceleration = 10, 9.8

or just use plain

speed = 10
acceleration = 9.8

I know that the two are not identical and that in the first version all the expressions are evaluated before the assignment and so it would make sense to use the first variant for cases where this distinction effects the results. (e.g. for swapping value a,b=b,a is just great).

In cases where the choice between the two variants does not change the results, when would you still go with the multi assignment and when will you consider multiple assignments to be more in the spirit of python ?

Upvotes: 1

Views: 1341

Answers (3)

jeremye
jeremye

Reputation: 1388

As with most questions like this, it's all about readability and changeability.

Assigning each variable on its own line usually scores higher for changeability, but there are a few cases where multiple-assignment makes understanding easier (in my opinion) by conveying relationships through proximity.

For example:

coords = (18, 32)
x, y = coords

or:

string = "I am a big string."
a, b, c = string[:7], string[7:11], string[11:]

But, of course, it's all situational, and I would err on the side of one assignment per line if you're in doubt because it's easier to change later.

Edit: I forgot one major reason not to do multiple assignments per line, and I just recently ran into the problem so I thought I'd update the answer.

It's harder to debug if an error occurs on a line with multiple assignments. Sometimes you can be assigning values to multiple variables using a complex list comprehension and several function calls, and it's just extra work to separate the lines and debug again to figure out where the problem is occurring.

+1 for individual assignments.

Upvotes: 2

javidcf
javidcf

Reputation: 59701

I think this is "primarily opinion-based", but if there is no special reason to use multiple assignments (such as the swapping example or return value unpacking) I'd stick to the simple way because:

  • It is harder to miss the assignment.
  • It is easier to read (less surprising or confusing) for people coming from other languages.
  • While it may be okay for a couple of variables, it doesn't really scale much further. Three is surely too much in most cases. So your code convention would have to be something like "assign variables in one line if you have a couple of them or each in one line if you have more", or "assign variables in pairs" (?).
  • Changing a value for one of the variables doesn't affect the line of the other variables (which can be seen as positive from a source control perspective).

That said, there are cases where I think multiple assignment may be better. It is rather subjective, but if I think the values are closely related (i.e. one doesn't make sense without the other), then I'll probably put them in one line. Examples include minimum and maximum values in a range or a set of three coordinates. There are less clear-cut cases, like "coefficients in a polynomial" (which are tightly related, but their meaning and relation is much less intuitive) or your own velocity and acceleration example, which are also very related but still do have a meaning on their own. Probably, my subconscious rule of thumb is that I may put them together if I feel they could belong in the same tuple or namedtuple.

Upvotes: 3

Yong Li
Yong Li

Reputation: 647

Swapping value would be a very convincing reason to do this. But for simple value assignment, I would go the 2nd way. It's easier to read (image if you have 10 variables).

Also it's easier to change. (Again if you have 10 variables, and now you need to change the value of 6th variable).

Also from editing perspective, editing each line is much easier than trying to find variable with its value...

Upvotes: 0

Related Questions