A PHP Guy’s Look At Python
Against all odds, I found myself with a little spare time this week. Rather than do something sensible like clean the garage or get some exercise, I took the opportunity to learn a new programming language: Python.
Like may SitePoint readers, I cut my teeth on PHP. I’ve become very comfortable with it over the years, warts and all. PHP continues to be a dependable choice, but PHP hasn’t changed a whole lot lately. Meanwhile, the kinds of applications I’ve been working on have been growing dramatically in both size and complexity.
Python has a lot in common with PHP: it’s a dynamically typed, open source scripting language with excellent documentation and a thriving community around it. Both languages are also a little quirky when it comes to their handling of Unicode text.
Unlike PHP, Python wasn’t originally designed as a language for Web development—it’s a general programming language that just happens to have some excellent libraries and frameworks for building web sites, like Django. This may sound like an argument against Python, but it turns out that when you start writing bigger web applications, most of your code has nothing to do with HTML, and PHP’s HTML-friendly features just seem to get in the way.
Enough Hand-waving! What’s The Code Like?
Let’s look at some of the neat features of Python code.
The most distinguishing feature of Python code is the lack of braces or other delimiters around code blocks. Instead, Python uses the indenting of your code to indicate blocks:
print "Let's count to ten!" for i in range(10): number = i + 1 print number print "All done!"
The above code includes a
for loop that will run ten times. The two lines following the
for statement are indented to indicate they’re within the
for loop. The last line isn’t indented, so Python knows the
for loop is finished.
At first it feels frighteningly fragile to trust indentation to describe the structure of your code; but once you get used to it, you’ll notice that your code looks less cluttered. Even better, you’ll find it easier to read Python code written by others, because developers are forced to indent their code neatly and consistently.
Python has a number of convenient, little features that make common tasks less cumbersome than in other languages. Take multiple assignment, for example, which lets you avoid creating temporary variables:
a = 1 b = 2 print a, b # prints '1 2' a, b = b, a+b print a, b # prints '2 3'
Python also has a number of slick features for dealing with what it calls sequences. These let you split and combine lists (the equivalent of PHP arrays) and text strings using simple, consistent syntax, instead of having to remember obscure function names for each.
Python’s list comprehensions let you quickly build complex lists out of simple lists with a minimum of code. They’re a little hard to understand at first glance, but they quickly become indispensable:
me = 'Kevin Yank' range(5) # [0, 1, 2, 3, 4] [me[x] for x in range(5)] # ['K', 'e', 'v', 'i', 'n'] [x for x in range(10) if x % 2 == 0] # [0, 2, 4, 6, 8] [me[x] for x in range(10) if x % 2 == 0] # ['K', 'v', 'n', 'Y', 'n']
But all that syntactic sugar aside, the biggest feature of Python that I appreciate after years of coding PHP is its sensible system of modules and packages for organizing code into multiple files. In PHP, when one script
requires) another script, it runs the risk of having its own variable, function, and class names clobbered by the script that it is including.
Python automatically sets up a separate namespace for each file (called a module) that your program imports, so that naming conflicts are naturally avoided. This frees you up to choose shorter, more natural names (especially for classes in big projects), while at the same time forcing you to give more thought to the structure of your code.
Where to Begin?
There is little sense in learning a new language unless you have a reason to use it. For the past couple of years, that reason for many web developers has been Django. The best place to start, therefore, is probably SitePoint’s Django Djumpstart article.
More recently, a lot of developers are taking renewed interest in Python because it is the development language for Google App Engine. It’s worth having a look at App Engine before diving into Python.
Speaking of diving into Python, Mark Pilgrim’s book Dive Into Python is available to read online for free, and is a great way to learn the language from scratch. If you’re after something with more depth and detail, the official Python Tutorial is a rewarding read, if a bit dry. It’s also significantly more up to date.