Quantcast
Viewing all articles
Browse latest Browse all 3

Python features for a PHP refugee

These are things that particularly impressed me when I decided I had had enough of PHP and I really ought to look at the crazy white-space language called Python that was used by Plone, Trac and Django.

The Zen of Python states most of this in 19 lines, for all you tl;dr types.

Name-spaces and a minimal set of built-ins

I like that the set of keywords is small, and that the built-in methods are not much larger. This leaves you with an unpolluted name-space (and if you enjoy confusing people you can always override the built-ins).

Explicit versus implicit

Related to name-spaces is the notion that Python is explicit: there is very little magic in a Python script. Perhaps the closest thing to magic are the various special methods that define a behaviour, for example the __getattr__ / __setattr__ / __delattr__ methods on a class to control attribute access. But even then Python makes it obvious those methods have special meaning by using a double-underscore for the method names.

See the page on the data model in the Python documentation for a description of these methods and their purpose.

Generators and list comprehensions

I never realized how much I missed these until I went back to PHP for a small web project. So much of my code seems to be looping through lists and accumulating a result or applying a function to each member of the list. I don’t think the syntax is particularly obvious, but then I can’t think of a better way to do it. At first glance generators looked to be the same as list comprehensions, but eventually I began to understand the difference between needing a finite list of objects (list comprehension) and consuming a sequence of objects (generators).

Named arguments for methods and functions

Gosh, not having named arguments is painful. As a consumer, named arguments allow one to forget a function’s precise argument signature, and as a designer it allows one to provide sensible defaults and flexibility.

Dates and times as a native type

Well, not native, but readily available.

Python’s datetime module provides representations of calendar dates and times and a bunch of obvious behaviour for comparing two moments. PHP 5 introduced a proper DateTime class, but I had jumped ship a while before then – my affection for Python’s date handling is borne of a time when one had to rely on PEAR for useful date functions. Converting everything to seconds since the Unix epoch was never fun.

The greatest annoyance in Python’s date implementation is its shrugging support for timezones – you nearly always need to resort to a third-party module (such as pytz or python-dateutil) to handle timezones without jeopardizing one’s sanity.

Batteries included

It is odd that one does need an additional module to handle timezones seeing as the Python standard library includes so many useful modules for common tasks.

Need to work with CSV files? Or command-line arguments? Or Mac OS X-style .plist files? Or configuration files in INI format? Or tar archives (with gzip or bzip2 compression)?

Oh golly so much tedious work has been done for you in the Python standard library. I suppose this reflects PHP’s emphasis as a scripting language for the Web versus Python’s use as a general purpose language, but I am very grateful for that distinction.


Viewing all articles
Browse latest Browse all 3

Trending Articles