I’m learning Python

May 31st, 2008

Google opened up App Engine to all developers this week, so I decided it was an ideal time to check out Python.

This was something I’ve avoided until now for a few reasons; primarily because I didn’t have the time to learn a new language, but also because I thought I wouldn’t like the semantics and philosophy of Python. I’m certainly more enamoured with the language now, but there still some ideas I’m not totally sold on just yet.

Semantics

Python has a really clean coding style (it’s even tidier than Ruby). For example, a simple class definition looks like this:

class MyClass:
	aString = ''	# public variable
	__anInteger = 0	# private variable

	def __init(self):	# constructor(-ish) method
		pass

	def Hello(self):	# class method
		return 'Hello, World!'

As you can see, Python uses indentation as opposed to braces in C-style languages, or start/end blocks in Ruby. I wasn’t keen on this before, but after playing around with it for a week or so I think I like it. I still don’t think it makes the code that much simpler to read, but it certainly makes coding a lot faster.

Philosophy

There does seem to be one overriding philosophy evident in Python’s design: there are few rules, but many conventions. For example, private variables and class methods are not really private, just obscured. I don’t understand why this is the case: if a method is private, it’s private for a reason, there should be no cause to allow a developer to have access to it. Annoyingly, Python also doesn’t allow for protected members. Python has special __init__ methods instead of constructors (for all intents and purposes, they operate the same way) and no destructors. Otherwise, it behaves pretty much like you’d expect an interpreted, modern programming language to operate (aside from, of course, the aforementioned private members idiocy).

The Standard Library

So, Python as a language isn’t really ground-breaking. What sets it apart from the rest is it’s fantastic standard library (and also it’s adaptability; Python can be shaped to suit almost any task). The developers like to say that Python ships with ‘batteries included’; it’s an apt analogy. I won’t go into detail regarding the standard library (because it’s simply too exhausting to discuss it thoroughly), but if you want to know more it’s very well documented at python.org.

Learning Python

Learning Python is exceptionally easy to learn if you’ve ever programmed before., it really is a testament to the language’s simplicity. There are hundreds of tutorials on the web and elsewhere that will get you started, but I’d point you towards the free (cc licensed) book available diveintopython.org. You can download it or view it online, or even order a printed copy from Apress if you want to support the author.