Becoming a Parselmouth

Learning a new language always comes with some kind of a learning curve. Patterns have to shift; expectations occasionally have to change; luckily, logic mostly stays the same. Learning Python after Ruby and Javascript has been relatively easy, but there were a few things that I got caught up on in the process.


The first thing that I’m sure everyone notices when starting Python is the indentation. It looks similar to other languages because of the way other languages also try to adhere to standards. In Python, the indentation is valued both indirectly through the culture of readability and very directly in the way the compiler parses code. I’m sure there have been thousands of annoyed or defensive blog posts written about this, so I won’t dwell here, but it is initially offputting that the compiler enforces correct indentation and extra whitespace rather than leaving that to disgruntled reviewers. And because I’d grown used to looking for brackets in Javascript or the ‘end’ keyword, it took some adjusting to only look for indentation. Everything looked so incomplete!

Screen Shot 2016-02-29 at 10.50.52 AM

This seems to check out

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

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.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s