I'll be the first to say that there is no 'perfect' programming language. The very thought of a 'perfect' anything is difficult to explain, and of all things, programming languages are among the least applicable to standards of perfection.
Nevertheless, there are clearly ways we can compare languages according to limited domains. A programming language, at its core, is an abstraction, and abstractions exist to make life easier for the person doing the abstracting. As such, we can judge programming languages based on the abstractions their designers chose to make and reach conclusions about which languages are appropriate for particular kinds of abstractions.
In additional, several overarching themes have arisen in Computer Programming, themes and paradigms which programmers should be aware of for historical reasons, even if they are not used in many or even a few modern projects.
In order to teach these concepts to to Computer Programming students, I advocate the use of the Python programming language. While I can't claim that Python is perfect or even ideal, it is better than other languages by a wide enough margin to make it what I could consider to be the clear choice for teaching computer programming.
I'm going to discuss these reasons for using Python:
- Python's clear, readable syntax
- Compactness of Language
- Pervasive Object-Orientation
- Quality of Documentation
- Sane Portability
- Ease of Packaging
- Widespread use of Introspection
- Immediate Practical Use
Syntax
Python was not the first popular high-level scripting language, but it was first among such languages to treat whitespace as significant. While many programmers find this uncomfortable, this actually makes a lot of sense for students. Students may not have the discipline of professional programmers about enforcing code style standards, and students may struggle to understand why indentation is important when working with a C-family language. Python, however, instills the "good habit" if indenting code according to logical structure.
Python's syntax is made clear not only by the use of indentation as block delimiters, but also by the relative lack of punctuation. Where other languages would use the familiar curly-brace syntax to denote blocks, Python uses indentation, and thus does not need the braces. In addition, by assuming each newline to be a complete statement, Python programs are relatively devoid of semicolons that would normally dot each line anyway. That being said, multi-line Python statements are usually not disruptive; implied line continuation inside of braces or parentheses makes for natural-looking multi-line syntax, while a simple backslash can always escape a newline in code, although this is rarely necessary.
It may seem odd to some programmers that such emphasis is placed on, ultimately, what the source code looks like. However, the cleanliness of source code is not something that should be dismissed; unlike many other languages, Python code that looks nice is usually code that also does what is intended. It is often possible to judge source quality simply by seeing if the indentation seems natural; if the program has extensive levels of indentation, or one-line nested if statements, programmatically dubious constructs also look visually alerting.
Another way of getting a birds-eye view of Python code is to, well, get a birds-eye view, literally by making the font extremely small. Python seems to form into a "map"; you can actually roughly make out what the design of the program is by looking at unreadable, but highlighted, source code. When doing this, braces are actually a liability; it requires either one or two additional line per code block, resulting is more sparse code. I know that a Python class is too large, or at least large enough to be likely to be a source of problems, when I cannot see it on one screen; in C-family languages, one screen is hardly enough to start laying out a structure.
Compactness
Python's keyword namespaces are implemented using Python's native dictionaries, which makes introspection particularly easy. The builtin functions are surprisingly few; the number needed for beginning and moderate Python programming is even lower. This language simplicity helps reduce the number of "edge cases" that cause frustrating, hard-to-catch bugs.
Python's keywords also often take the form of English words and usually have the expected semantic meaning; Python's for loop syntax, for example, is blessedly simple:
- for variable in iterableVariable:
- # loop_body
Python uses words instead of symbols in many situations: && and || are eschewed in Boolean statements in favor of the clearly more natural 'and' and 'or.'
Object-Orientation
Novice Python users will usually get their first experience with Objects in the form of File objects; the introduction to instance methods and attributes can easily come in the form of .read() and .name. Indeed; these make for a very good introduction, as they are actually-used, practical uses of an object, as opposed to the contrived, confusing and constructed examples many textbooks use to describe OOP.
Once students have begun to write their own classes, Python also allows them to focus on logic rather than idiosyncrasies. Simply put, learning programming has nothing to do with writing get and set methods.
Documentation
Python's documentation is, all told, very good. In part, this is simply a result of Python's maturity; it has been around for long enough to have accumulated a good documentation base. Python's (large) Standard Library is well-documented and current; the default tutorial is an excellent introduction, and often a good reference as well. For moderate to advanced topics, the Language Reference provides comprehensive syntax and keyword documentation, while the Library Reference covers all but the most obscure corners of the standard library.
The other important source of documentation are Python Enhancement Proposals, or PEPs. The first PEP I ever saw was PEP-249 when I first started using MySQLdb. Again, I would say that this would be an ideal reference for a student first learning database access from a programming language.
Portability
I'll openly admit that when I first starting programming in Python, I saw some of the "Availability: IRIX" labels in the documentation and thought to myself, "What's an IRIX?" In fact, for most programming students, cross-platform coding probably isn't high on their list of priorities. Nevertheless, learning that portable programming is not just a language or a runtime, but is rather a 'thoughtful' approach to programming, is one of the most highly-rewarding concepts a programmer can learn. I think it's important for students to know that Java is not cross platform if you hard-code a "C:\" filename into it; neither is Python, for that matter. But Python makes it easy, if not transparent, to do the extra bits necessary to make your program run everywhere.
One of the 'coolest' moments in using Python for me was when I started using PostgreSQL, and I found that 90% of the changes I had to make in the Python code was to change
:::python
import MySQLdb as sql
To:
- import psycopg2 as sql
I wouldn't say it was quite effortless, but it opened my eyes to the concepts, rather than just the patterns, of platform-independent programming. In this respect, Python has made be a better programmer in all languages.
Another very cool experience was writing a wxWidgets program in Python. The program had, of course, a GUI component, but also was networked, used SSL, XML-RPC, and pretty much every other trick I knew at the time. I had written the program without portability in mind, and predictably, when I put it on a USB key and tested it on a Windows computer, it didn't work. However, I went back through every line of source code and removed all Linux-specific filenames, os.system calls, and the like. At that point, the program worked perfectly on Win32, even with the server side still running on Linux. (The server relied on a database backend, so I didn't bother making that cross-platform - I knew I'd always be running a database on Linux anyway.)
Packaging
Packaging might seem like another topic that elementary Python students might not need to go too far in depth about, but, in fact, good packaging is what allows a curious beginner programmer to become a passionate moderate coder (or, at least, it did for me). In my experience, 101-level courses typically don't get beyond single-file programs. This is a gross mistake. If a student is trying to write something on their own time, they'll want to try different alternatives, and the size of their code will soon reach the point where they cannot reliably scroll through it all (and as mentioned above, vertical height of source code is more important than is usually admitted).
The good news is, Python's packaging is simple, and especially easy to pick up once functions and classes have been learned. There is no particular reason a Python student cannot be working with multi-file programs by the time they are working with multi-class programs; in fact, I would recommend it. In addition, packaging is something a professional programmer will be working with on a daily basis, making early familiarity and comfort with the packaging system important.
Introspection
Python's few language constructs and pervasive use of objects makes for a very effective and useful introspection system. A Python student can learn to learn; using the help() command they can explore classes within the shell and see how the Standard Library lays out code. Additionally, with the dir() command, students can more fully comprehend the idea of instance methods and attributes. By teaching these two commands early in a semester, an instructor can equip a student to be able to learn about things not in the textbook or things even not in the instructor's knowledge.
Practicality
Python, in addition to being a great teaching language, is extremely mature, of relatively high performance, and is widely used. By sheer virtue of being nearly as old as I am, has a stable language core, a tried and tested bugfix process, and a generally optimized interpreter. (the same, regrettably, is not true of me...)
Because of this, Python is finding lots of use as a general-purpose language for when developer time is at a premium, more so than CPU time. While for many situations this is naturally not the case, such as in embedded and hardware programming where C and C++ are still popular, Python is finding its priorities - developer time and easy debugging - of increased importance. In web programming especially, where the latencies of the network make CPU time pale in comparison, Python, along with other high-level, interpreted scripting languages, is making huge strides. However, unlike Ruby and PHP, Python is arguably finding more success outside of the Web 2.0 revival than inside. GUI libraries for Python are more widely used (and are more usable) than those of other interpreted languages.
Comments
85 spam comments omitted.
I am no longer accepting new comments.
M Easter
#281, 2007-09-03T13:25:37Z
Great post... if I had to only use one language for the rest of my career, it would be Python.
Anonymous
#282, 2007-09-03T23:30:46Z
It's weird, ever since getting addicted using Haml as a view framework on ruby on rails, I've longed for whitespace sensitivity in ruby. Its just another work item every time I finish coding to have to match up and format all the end statements.
But python seems majorly crippled in terms of using blocks.
My biggest complaint though is just the underscores everywhere. To me, it's a really ugly convention used to solve a problem that doesn't really exist.
Brian 'Psychochild' Green
#6166, 2008-08-23T19:15:48Z
A great overview of the strengths of Python. I'm a professional online game developer, and I really enjoy working with Python.
I'm currently writing an MMOG server setup entirely in Python. I've done a bit of contract programming for other companies and working in C++ is a lto more painful for trying to keep track of everything needed. The brittle nature of C/C++ code is also worrisome, because a server crash bug can cause a lot of problems when you're running an online game.
The syntax issue is also really nice when talking about larger projects. When I was maintaining Meridian 59, a lot of my time when into cleaning up the syntax, indentation, etc. of the code to just understand what was going on.
Finally, another great benefit if Python is its flexibility. For example, the Stackless variant (http://www.stackless.com) has been used by online game developers. The concept of tasklets (microthreads) makes things much easier to handle in a real-time, multi-user environment.
Anyway, Python has made me much more productive. I hope that it continues to improve! :)