Linters alleviate some of the pain of transitioning, so it won’t just show up in testing when it confusingly doesn’t compile. Our tests check Flake8, a style and syntax linter, before running, catching things only the most curmudgeonly of reviewers would. Luckily, there is a handy Flake8 extension for Atom that catches it before I spin up the tests. Atom also has some other nice features and packages, like getting rid of extra whitespace on save.
At this point, though, it seems nice that everyone has to remain relatively consistent with these practices rather than just hoping they will.
List comprehensions gave me my first wtf moment in Python. I’d been browsing some of the docs to get a feeling for how to work with data structures and saw this:
[ [ 1 if item_idx == row_idx else 0 for item_idx in range(0, 3) ] for row_idx in range(0, 3) ]
I can now tell it creates a 3×3 matrix where elements for which `item_idx == row_idx` is true are 1 and all the rest are 0. At the time, I had no idea where to start. I recognized the `1 if item_idx == row_idx else 0`, but throwing the `for … in …` in seemed like overkill in an attempt to make it a one-liner. Even Ruby programmers, who love one-liners, would generally frown on something as unclear as that. So, of course, I threw it into the command line and let it answer itself.
Now, I know to look at the end for the general structure of the statement and work backwards. From that, it’s clear that it’s going to start as a list of 3 elements, then get mapped to a list of 3 lists. It then pushes elements into the lists, setting some as 1 and some as 0. You then end up with three arrays where each has a single 1 at the index that matches its location in the main array and and a bunch of 0’s other than that. Even though I can look and figure this out now, I still generally avoid the syntax because of how much less readable it feels. I prefer to avoid spending even a few seconds on a single line trying to decipher it, so I’ve found more familiar methods like `map()` and `filter()` to which I can easily pass a lambda, instead.
At this point, I’ve found myself really enjoying working in Python. The community seems to have rigid expectations, but they are mostly reasonable for a newcomer to pick up over a short period of time. The more I read about it, the idea of only having one way to do any particular thing appeals to me as opposed to the JS convention of having a bajillion ways to do any particular thing, with differences that typically don’t matter until after a few days of work when it screws something up silently. It seems to me that list comprehensions are more like the JS style, the indentation thing is overkill, and developers (like me) can find a way to complain about anything. If we can all aspire to the Zen of Python (`import this` in a python interpreter), though, life will be easier for all of us.