blogroll tags

On the evolution of snakes.

It's been a number of years since I first learned programming in Python with Mark Pilgrim's excellent, but now somewhat outdated, book, Dive Into Python. It has managed to become outdated because the Python language is being developed and improved all the time and new features are being added. One of the best features of Python is, beside the standard libraries, arguably, the documentation, which is good enough to include What's New documents for every release.

I've decided to have a look at the backlog of new features, and consider how I use Python today in ways that simply didn't exist when I originally came across the language.

Read about my findings after the break. (Technical language is used. Knowledge of Python and its features is presumed.)

The first Python version I used was probably Python 2.3, possibly version 2.2, so I won't be looking any further back than that. What was on by default in Python 2.2 had always been there, as far as I'm concerned.

from __future__ import division

The behaviour of the «/» operator has changed since Python 3.0, the new behaviour is also available in versions since 2.2. I still haven't quite come to terms with this not only because the new behaviour violates a fair amount of experience in many, many languages (/ -> ]]> ?), but also because I find it highly inconsistent: while the «/» operator now always returns floating-point numbers, the new «//» operator (“integer division”) only returns integers when both arguments were originally integers as well. Moving on ...

from __future__ import generators

Generators and iterators. I love them. Introduced in Python 2.2/2.3, these weren't ever really new to me in any way Python itself wasn't, but they still deserve a mention. Thanks to the flexibility of the for thing in thing_that_includes_things syntax, Python is a wonderfully expressive language for, say, data processing scripts.

PEP 289: Generator Expressions

Python 2.4 followed up generators with a device that allows you to use the expressive power of list comprehension in situations where memory constraints might previously have prohibited it: generator expressions. List comprehension without the, typically throw-away, lists.

PEP 318: Decorators for Functions and Method

Decorators were quite a surprising addition at the time. The way I see them, they're an amazingly powerful way to specify what a function is meant to do — the function body only specifies how it's done, after all. For example, Django allows using a decorator to specify that a function should be visible in templates as a filter (or tag).

PEP 308: Conditional Expressions

The long awaited murder of the and-or-trick. When I learned Python, I used and/or hacks “all the time” and didn't know what I was missing until Guido van Rossum shoved an expression that you can actually read down my throat. Example:

>>> # compare this
... length = (done() and [0] or [len(chain)])[0]
>>> # with this
... length = 0 if done() else len(chain)

from __future__ import print_function

Python 3.0 does away with the old print statement. About time too. Never again will Pythonistas be forced to write functions like this for passing around functionally:

def print_wrapper(txt):
    print txt

Is that all?

Of course it's not. There have been many, many other changes that I haven't mentioned here. All in all, I love not only Python itself, but also the way it's developing. Finally, I want to mention two new syntactical nova that I don't really have any experience with, but that can certainly be great enrichment to the language when used sensibly: The with statement (Python 2.5/2.6) and annotations (Python 3.0).

:
:
:
: