logoalt Hacker News

kccqzytoday at 3:28 PM1 replyview on HN

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]

Replies

PurpleRamentoday at 4:42 PM

> 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.

show 2 replies