I've been a professional C, Perl, PHP and Python developer.
I'm an ex-sysadmin from the late 20th century.
These days I do more Javascript and CSS and whatnot, and promote UX and accessibility.
I don't like this sort of thing for assignments. I think readability suffers too much.
It makes it harder to see at a glance that loc2 and program[pos + 2] are the matching things where the assignment is happening. It gets worse when the names are long (like in this example) and should be wrapped across the next line.
It also makes it harder to merge changes if things aren't kept to one assignment per line.
Interesting point. Would you reccomend doing the assignment on four lines? I do see the readability advantage of that.
As I look back on this, assigning the loc variables does more for readability then the tuple assignment does.
I think it's worth adding that this is something Joel Grus hacked to gether, so I don't know whether he would write code this way in all situations.
Most of my projects are on Bitbucket at the moment but will be moved to GitHub soon due to the shutdown of mercurial support.
See: https://bitbucket.org/labscript_suite/
Location
Australia
Education
PhD (Physics), BSc Advanced with honours (first class honours in Physics, majors: physics, maths)
Given the data is in a list, and sequential, it would be clearer to just directly unpack the list, rather than using an intermediate tuple:
opcode, loc1, loc2, loc3 = program[pos:pos+4]
I think that's readable. But if there isn't an existing relationship between the values, assigning across multiple lines is definitely better for the reasons Ben suggested.
I don't like this sort of thing for assignments. I think readability suffers too much.
It makes it harder to see at a glance that
loc2
andprogram[pos + 2]
are the matching things where the assignment is happening. It gets worse when the names are long (like in this example) and should be wrapped across the next line.It also makes it harder to merge changes if things aren't kept to one assignment per line.
Interesting point. Would you reccomend doing the assignment on four lines? I do see the readability advantage of that.
As I look back on this, assigning the loc variables does more for readability then the tuple assignment does.
I think it's worth adding that this is something Joel Grus hacked to gether, so I don't know whether he would write code this way in all situations.
Given the data is in a list, and sequential, it would be clearer to just directly unpack the list, rather than using an intermediate tuple:
opcode, loc1, loc2, loc3 = program[pos:pos+4]
I think that's readable. But if there isn't an existing relationship between the values, assigning across multiple lines is definitely better for the reasons Ben suggested.
Thanks.
That's clever, and I agree it's readable.