I have been cultivating a real appreciation for Python over the last year or so, and am trying to immerse myself more deeply in the hopes of more consistently using it as part of my paid work. This is my first post about it, so I thought I’d give an overview of my impressions so far.
One of the most common contexts in which I hear Python lauded is as a language that’s friendly to newcomers. Often cited is its emphasis on prioritizing readability. I’ve been skeptical about this kind of assertion from the start, but couldn’t really articulate why until recently.
Certainly Python has many of the hallmarks of a good introductory language:
- It uses English-like operators pretty much wherever possible.
- It enforces indentation and other formatting that make it readable.
- It doesn’t bog down code with lots of strange characters.
- It makes command-line input and output very simple.
(And many more!)
But it also has characteristics that can be really complicating for the beginner or casual programmer, for example:
It supports modular, object-oriented, and functional programming models indiscriminately and interchangeably. This can make it really confusing to navigate.
It supports some syntactically complicated things like method chaining and multiple assignment that make code hard to follow if you aren’t already used to that paradigm.
I would argue, for example, that this code:
mystr = ' This is a string THAT I wrote! ' thatloc = mystr.strip().lower().find('that')
Is infinitely harder to follow for a new programmer than:
mystr = ' This is a string THAT I wrote! ' mystr = mystr.strip() mystr = mystr.lower() thatloc = mystr.find('that')
Even though the former is unquestionably more efficient to write, and easy enough for an experienced Python programmer to understand.
It has strongly typed variables that are not explicit, making type problems non-intuitive for the student. Here it occupies a middle ground between an explicitly-typed language like C and a much looser language like VB and that middle ground is kind of weird (more on this weirdness later).
While a very high-level language, it has pockets of low-level usage scattered throughout. For example, it uses zero-based indexes for lists and tuples.
The more I use Python and get used to its quirks, the more I get the feeling that it occupies a very strange space: at its core, it presents a simple syntax that is uncomplicated enough for the most unseasoned beginner, but it packs a tremendous amount of power into the core language, and much more with even just the standard library of modules. While the core functionality makes it seem like a language intended for learning, like old-school BASIC, it also has the features to do almost anything. There’s a strange mix of simplicity and functionality that can be disorientating for the student of the language. There are times when it feels like a high-level language for people who like the concepts of a much lower-level language, like C. In fact, even though there are other implementations, the most common Python is written in C and has libraries written in C.
The standard library shows its roots in places. If you want to get at command line arguments for your script, you can use
sys.argv, which is very much analogous to the
argv used in C. You can also use the
getopt module, which has the stated purpose of being similar to a C function (
getopt()). The difference, of course, is that Python now has a much more fully-featured module,
argparse, that makes support for complicated command-line arguments relatively simple to implement. And this is, I think, the source of the strange feeling I have about Python: many of its features began as analogues for C implementations, but over time the library has expanded to provide higher-level versions of the same functionality. Which is to say that, as much as Van Rossum and co. made Python with simplicity and friendliness toward newcomers in mind, it often feels in practice like a high-level language for experienced programmers. Not that I’m complaining it’s precisely that feeling of powerful simplicity that has me hooked on the language. But it does make me wonder about Python as an introductory language. Here I have a hard time, because there’s always a part of me that thinks C is a great learning language that starting with a language that doesn’t hide much under the covers will teach the student about how programs fundamentally work and will pay dividends for their whole programming life. There are times when the only way I can get my head around why Python does something the way it does is my background knowledge about C and about how computers work.
I’ll acknowledge these things, and let others decide the right language to be their first:
- Python’s popularity exists for a reason. It makes me remember the fun of programming.
- There’s not much you can’t do with it. It’s not a toy language by a long shot.