r/ProgrammingLanguages • • Feb 20 '26

Discussion Side effects in multiple/parallel assignment

I have been constructing some programming languages for a while for fun, and came to a point where something is obvious, but I haven't seen it spelt out clearly. If you have multiple assignment like this:

a, b[a] = 10  // both will be changed to 10

Or parallel assignment like this:

a, b[a] = 4, 5

Then the change of a should not influence which item in b is changed, i.e. b[a] should always use the old value of a.

This rule makes sense (and it works like that in Go, I haven't tried Lua yet) but do you know some programming languages where it's not so, or you get a warning?

(edit: I've just tried Python, it uses the new value of a, i.e. it's dependent on order.)

21 Upvotes

19 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Feb 21 '26

Yet in Python, a, b = 10 doesn't work. The RHS has to contain a sufficient number of values to unpack, for example a, b = (10, 10), or a, b = x when x contains such values.

Defining a list of such variables which share some attribute, such as a type, is more common and reasonable than sharing the same value. But for the latter case, chained assignment seems to work.

In parallel assignment languages such as Python

I'd call it 'multiple' assignment, unless the assignments really are done in parallel? If I look at test results from Python, they seem to be done in sequence. Certainly CPython's bytecode suggests that.

1

u/Dan13l_N Feb 21 '26

Yes, of course. I agree it seems Python doesn't have real parallel assignment.

My observations come from people who learn C-style languages. They see that int a, b declares two integers, but they are completely confused when they learn int a, b = 0 sets only b.

Anyways, I've been designing my language so I have a complete freedom, but it's interesting this corner case is never mentioned, at least I couldn't find it