Live data from Hacker News

Chained Assignment in Python Bytecode

loriculus.org

1–10 of 18 posts

Re: Chained Assignment in Python Bytecode

#2
> list object is constructed once and assigned to both variables

Ummm no the list is constructed once and assigned to b and then b is assigned to a. It would be crazy semantics if `a = b = ...` meant `a` was assigned `...`.

Edit: I'm wrong it's left to right not right to left, which makes the complaint in the article even dumber.

Re: Chained Assignment in Python Bytecode

#3
Chained assignments are banned according to the style guide at my workplace. Too many opportunities for misuse. And if you insist on a one-liner assignment to two variables just use two statements separated by the semicolon. I challenge anyone to work out what this code does:

    a, b = b[a] = 1, [0, 1, 2, 3]

Re: Chained Assignment in Python Bytecode

#4

> list object is constructed once and assigned to both variables Ummm no the list is constructed once and assigned to b and then b is assigned to a. It would be crazy semantics if `a = b = ...` meant `a` was assigned `...`. Edit: I'm wrong it's left to right not right to left, which makes the complaint in the article even dumber.

> then b is assigned to a

Wouldn't that require a LOAD_FAST? Also a is assigned first (from left to right) so a = ... happens either way.

Re: Chained Assignment in Python Bytecode

#5

> list object is constructed once and assigned to both variables Ummm no the list is constructed once and assigned to b and then b is assigned to a. It would be crazy semantics if `a = b = ...` meant `a` was assigned `...`. Edit: I'm wrong it's left to right not right to left, which makes the complaint in the article even dumber.

It’s assigned left to right, not right to left. It’s documented in the Python language reference.

> An assignment statement evaluates the expression list and assigns the single resulting object to each of the target lists, from left to right.

Consider this:

    a = [1, 2]
    i = a[i] = 1
If assignment were to happen right to left, you would get a NameError exception because the first assignment would require an unbound variable.

Re: Chained Assignment in Python Bytecode

#6
The author's expectations seem strange. Take another example:

    a = b = random.random()
I would not expect a and b to get different values. It would be very strange if using `[]` had different behavior than a function call in the same place. Am I out of step here?

Re: Chained Assignment in Python Bytecode

#7
post #5

> list object is constructed once and assigned to both variables Ummm no the list is constructed once and assigned to b and then b is assigned to a. It would be crazy semantics if `a = b = ...` meant `a` was assigned `...`. Edit: I'm wrong it's left to right not right to left, which makes the complaint in the article even dumber.

It’s assigned left to right, not right to left. It’s documented in the Python language reference. > An assignment statement evaluates the expression list and assigns the single resulting object to each of the target lists, from left to right. Consider this: a = [1, 2] i = a[i] = 1 If assignment were to happen right to left, you would get a NameError exception because the first assignment would require an unbound vari…

Fine but that even moreso illustrates how goofy the expectation that the "ctor" for [] would be called twice.

Re: Chained Assignment in Python Bytecode

#8
post #3

Chained assignments are banned according to the style guide at my workplace. Too many opportunities for misuse. And if you insist on a one-liner assignment to two variables just use two statements separated by the semicolon. I challenge anyone to work out what this code does: a, b = b[a] = 1, [0, 1, 2, 3]

> I challenge anyone to work out what this code does

It's unusual, but pretty obvious. In single steps it's basically this:

    a, b = 1, [0, 1, 2, 3]
    b[a] = b 
which would be b[1]=b because a==1. So this creates a self referencing list. The deeper reason here is, everything is a pointer to data in memory, even if in source code we see the actual data. That's why b[1] is storing the pointer to the list, not the data of the list.

If someone is doing BS like this, they deserve spanking. But banning the whole concept because people are unaware or how something is to be used properly is strange. But then again, it seems people have a bit of a problem how python really works and how it's different from other languages.

Re: Chained Assignment in Python Bytecode

#9
post #3

Chained assignments are banned according to the style guide at my workplace. Too many opportunities for misuse. And if you insist on a one-liner assignment to two variables just use two statements separated by the semicolon. I challenge anyone to work out what this code does: a, b = b[a] = 1, [0, 1, 2, 3]

> I challenge anyone to work out what this code does It's unusual, but pretty obvious. In single steps it's basically this: a, b = 1, [0, 1, 2, 3] b[a] = b which would be b[1]=b because a==1. So this creates a self referencing list. The deeper reason here is, everything is a pointer to data in memory, even if in source code we see the actual data. That's why b[1] is storing the pointer to the list, not the data of th…

An adjacent thread has some confusion about whether chained assignments happen left to right or right to left. Honestly that’s a factoid I don’t expect most Python programmers to know. It’s usually a bad idea to rely on people knowing arcade details of a language, especially a language like Python that has attracted many non-programmers like data scientists. (I have nothing against data scientists but their brainpower shouldn’t be wasted on remembering these kind of details.)

Re: Chained Assignment in Python Bytecode

#10
post #9

Earlier quoted context omitted.

> I challenge anyone to work out what this code does It's unusual, but pretty obvious. In single steps it's basically this: a, b = 1, [0, 1, 2, 3] b[a] = b which would be b[1]=b because a==1. So this creates a self referencing list. The deeper reason here is, everything is a pointer to data in memory, even if in source code we see the actual data. That's why b[1] is storing the pointer to the list, not the data of th…

An adjacent thread has some confusion about whether chained assignments happen left to right or right to left. Honestly that’s a factoid I don’t expect most Python programmers to know. It’s usually a bad idea to rely on people knowing arcade details of a language, especially a language like Python that has attracted many non-programmers like data scientists. (I have nothing against data scientists but their brainpowe…

I've been programming Python since 1.5.2 days and indeed, I didn't know the order of evaluation of chained assignments.

That said, it's the self-referencing list in your example that's the more confusing part. It's atypical to have self-referencing data structures, so that's something I'd comment in the design if I needed one.

Post reply on HN