python verses perl

Work has, for a number of reasons, decided to standardise on Python for coding, and Django as the web framework. Note that this does not mean we are going to go mad & rebuild all our running services in Python/Django, nor are we going to follow an exclusive policy: there are definitely situations where other technologies are more appropriate (so Perl, Java, Ruby, Javascript, etc are all still present - and have not been abandoned)

This affects me as I'm a Perl programmer: I have been for over 20 years, and pretty much exclusively Perl for a good 10 years (with some javascript thrown in for client-side web app work) - Python is something I need to learn.

Like many people - I don't learn by shutting myself away and reading a book.... I learn by doing (I've learnt my mechanic skills that way, I've learnt Perl that way, and I've learnt my teaching skills that way - Go Mr Practical)

I have just had the opportunity to do that: I have three pieces of work that are all awaiting feedback from user-testing or responses from various management groups - and I had a window of several days that were probably going to be clear. This gave me the opportunity to re-work a simple prototype I'd written in Perl, in Python. The code grabbed some data from a remote API, cross-walked the XML to create a subset of the data, which it returned. The remote API paginated it's response (so the code needed to itterate to build up a complete response) and had a small set of optional parameters that could filter the returned data

So how do they compare?

CAVEAT: this is after a few days recreating a specific wee bit of code, and by no means an exhaustive comparison!

Well, fundamentally..... there's not a massive difference between them.

OK, so there's "the pythonic way" and "the perl way" - and I've 20 years of wisdom to learn wee tricks & nuances in Perl that I just don't have for Python.... but code design is code design: and the two languages are close enough that I can take the basic design from the Perl code & re-create it in Python.

  • I keep typing "pythin" rather than "python" - but that my just be my fat fingers
  • Python encourages lots of small functions - I don't know why.... it just does
  • Python passes all function variables as "by reference", rather than Perls "by value"
  • Perl makes it much easier to set up functions that take a variety of [named] parameters (pass in a hash)
  • Python has this clever thing where parameters can be both positional, and named.... and use both, simultaneously
  • Python code looks cleaner (or maybe that's just "simpler"?)
  • Python has some weird backward compatibility issues - for example the "print" statement does not use braces in 2.7, but requires them in 3.4; whereas perl added "say" to give subtly different functionality to "print"
  • Python code is really hard to re-format (as in "lint", "htmltidy", or "perltidy")
  • Perl allows quick'n'dirty coding, it allows sloppy coding, and it it really easy to automatically re-format into a "sensible" presentation format (as in "lint" or "htmltidy", I use "perltidy")

Python has some weird conventions - like a regex match function specifically for the start of the string (rather that anywhere in the string) as well as PCRE characters that signify "start of string" within the regex itself

It will be interesting to try doing a larger piece of work in Python, and it will be interesting to work with someone else who's an experienced Pythonist.

To view or add a comment, sign in

More articles by Ian Stuart

Others also viewed

Explore content categories