Most of my students know I’m sold on the Python programming language. It’s been my language of choice since the early part of this decade. Before that, I had programmed extensively in C++, Java, Object Pascal (Delphi) and a number scripting languages. However, I have settled on Python for the time being, and nothing that has come along in almost 10 years has persuaded me to jump.>
Why Python? There are a host of reasons, but the primary reason is I’m extremely productive in it. Python is both rapid development, concise, elegant, and powerful; where other languages choose between these, Python has somehow achieved them all.>
For those investigating Python or just trying to determine a primary scripting language to focus on, the following is my list of the ups and downs of Python.>
- Python’s community/culture prides itself in creating beautiful, concise, readable code. Python’s use of white space for blocks (rather than curly braces) reinforces this on every Python programmer. The culture is to find the right way of doing something, and do it once that way.
- The language itself is clean and well-designed. I’ve taught my 11 year old and 9 year old Python, and they had no trouble with the language (check out RUR-PLE. Compare that with other languages, where the syntax itself confuses new learners. Python uses white space for blocks, has no semicolons to end lines with, has no $ in front of variables.
- Python works identically on all major operating systems. Since I regularly use Linux, Mac, and Windows, this is a must.
- The language "enforces" things by convention rather than compiler. For example, interfaces are implied rather than explicit and private variables are not explicitly private — they just have special names so programmers know to stay away from them. Enforcement by convention results in two advantages: 1) code is much, much simpler and concise because things like explicit interfaces don’t have to be created, and 2) advanced programmers can break the rules when it makes sense to do so. Just as there are times when speeding on the highway is the right thing to do, Python allows me to break the rules if I desire. While this could allow a language to spin out of control, Python’s clean-code culture has kept it well in check.
- Python comes with "batteries included". It has an extensive library with methods to do most common things. The library modules are programmed to the 80 percent normal use already, and allow freedom for the programmer to do extra work for the 20 percent use that requires it. Sorting, for example, is accomplished with a simple list.sort() method (the 80 percent use), or the programmer can plug in a function to sort in a specialized way (the 20 percent use). Java, on the other hand, requires creating a separate class and set of methods *every* time I want to sort a list.
- Python has implicit data typing. In reality, variables are simply pointers to objects, and even the primitives like integers are objects that can be extended. This lack of explicit typing allows polymorphism at a much higher level than languages like C++ or Java. If a function takes a list, I can actually send in *any* type of object, as long as it looks enough like a list for the function to use it (more specifically, the object just has to have the methods used by the function).
- I can modify objects wherever and however I want. For example, if a built-in object needs a new method for my program, I can add it on the fly — after the object’s creation. Java accomplishes this with the clunky obj.setAttribute() method. Python is much more elegant because the extra variables or methods can simply be added directly to the object. This ability allows me to overload anything I want in superclasses, even if I don’t have access to the source code of those superclasses. Again, one might think this would cause confusion in the language, but the clean nature of Python code and community keeps its usage to places where it really does make sense.
- Lists and dictionaries (aka maps) are used extensively throughout the Python language and libraries. For example, a Python string can be treated as a list (of characters), which means any method that works on lists can also work on strings. Lists, in particular, are extremely useful; they have methods of pop() and push() to emulate stacks; they can be accessed in reverse with mylist[-1]; they can return slices (pieces of the list as new lists); they can be joined into or split from strings.
- Python supports both procedural and object-oriented programming styles. It supports "modern" concepts like try/catch. Because it allows code outside of classes, it doesn’t need the weird "static" keyword that messes up new programmers in other languages.
- Python includes a built-in shell, which allows the programmer to issue commands one at a time and see immediate results (like the command line or terminal). This is more useful than you might think because it allows quick testing of code like string manipulation.
- Python was created by Guido Van Rossum. Does your favorite language have a cool name like that?
Despite my love for the language, Python has a few warts. Here are a few that bother me:
- Since Python enforces rules mostly by convention, it can give too much freedom to new programmers. Now, it’s more structured than spaghetti languages like PHP, but I actually prefer Java for new college students because it forces them to learn good principles (the compiler won’t allow anything else). In my view, Python is a great language for absolute new programmers and for advanced, experienced programmers. In between, students need a highly-structured language to solidify their principles.
- Python objects are extremely well done and straight forward — all except one thing: self. Self is the "this" keyword for Python, and rather than being totally explicit or totally implicit, it’s halfway. It must be included in method signatures, but it’s not included in the calls to those methods. It just takes a little getting used to, but it’s not as clean as it could be.
- Python has no standard GUI. And no, I don’t count Tk for its standard GUI (it’s too simple for real apps). The Python community has long argued over which GUI is best, so one toolkit has never taken hold. Personally, I think wxPython needs to take this place. It is mature and advanced, and the Python bindings are well done.
- Python has no standard web framework. While Java and PHP and .Net have integrated frameworks, Python has a number of them available. This may standardize through market forces eventually, but right now web programmers wanting to take advantage of Python have a lot of choices. Choice is good for advanced Python programmers, but it is confusing to new people who want something straight forward like PHP.
Python is a wonderful language that can be used from everything to simple scripts to full GUI or web programs. It can do almost everything Java or .Net can, but with a lot less code. As a concluding comment, here’s a few of my views on Python vs. today’s popular languages:>
- Python vs. PHP – PHP is an excellent web language, but it doesn’t have the breadth of Python. While PHP is specific to the web (and no, I’m not counting PHP’s "command line" ability), Python can create full GUI programs and small scripts, and it can even be embedded into databases like PostgreSQL. PHP also has a community made up of many self-taught web programmers, resulting in terrible examples on the web, tons of spaghetti code, SQL embedded into web code, and other problems.
- Python vs. Perl/Ruby – Python is much cleaner than Perl, and it has a different syntax than Ruby. Perl is an older language that I don’t recommend to people. It’s OO design feels hacked on because Perl wasn’t originally OO. In contrast, Ruby was designed from the ground up to be object oriented and to fix Perl’s problems. Ruby is not as popular as Python, but it’s the proper replacement for Perl and has a definite place in the world. I recommend it to anyone who prefers the Perl way of doing things.
- Python vs. Java – Both languages "feel" the same to me. Python is made up of a lot of Java defectors (like me), so its libraries (like the threading module) are often patterned after Java libraries. If you like Java but are tired of the large amount of code it takes to do anything, check out Python. In my view, Python is the proper replacement for Java in many ways. Java still has its place in highly-structured environments, but Python can do most of what it does in a tenth the code.
- Python vs. C++ – These are not really competitors. Python is a scripting language geared towards rapid development. C++ is a compiled language with much faster runtime speed. It is better suited for OS, driver, and hard-core application development. One of Python’s strengths is actually its ability to merge with C or C++ code seamlessly. It has an entire, built-in framework devoted to wrapping C code so it looks like Python code (wxPython is exactly this). This allows you to write C or C++ code where necessary, and Python code for the rest.
- Python vs. VB.Net or C#.Net – Microsoft’s Visual Studio is simply the best in class as editors go. If you are looking for a framework to automate many development tasks or even hand-hold you through the process, .Net is a good way to go. Python allows more freedom but doesn’t provide any tools like Visual Studio. Personally, I don’t use .Net because I don’t like to run Windows servers. Python lets me go to Unix, Mac, or Windows, depending upon what my needs are.
- Python vs. Bash or other shells – Modern scripting languages (like Python, Ruby, and Perl) can do everything the previous generation of shell scripting languages can do. Indeed, most Linux distributions use Python or Perl for their installations and system startups now. Bash is great for very simple things, but I push my students to real scripting languages now. I am looking forward to the day when shells like iPython (a full Python-based replacement for Bash) take hold.