Why should I switch to Python?

Hamish Lawson hamish_lawson at yahoo.co.uk
Sat Apr 1 16:41:49 EST 2000

Hi Aaron

Here are the reasons I switched to Python.

I had used Perl for several years and I think I had gotten to
quite an advanced stage with it. However what convinced me to
switch to Python was the recurring difficulties I saw my less-
experienced colleagues having with things like references and
other dark corners of Perl. Sure, it would have been much harder
to use C or C++ for the kinds of things we were using Perl for,
but I felt that the continual taking of references and
dereferencing was still too much like C's pointer hassle. In
Python, since all variables hold references, references can be
transparently handled.

Compare this in Perl:

    @people = ('Joe', 'Pat', 'Sue');
    @stuff = (5, \@people, 7);
    print $people[2]

with this in Python (square brackets are used to make lists):

    people = ['Joe', 'Pat', 'Sue']
    stuff = [5, people, 7]
    print people[2]

The first line of the Perl version causes @people to contain a
list, but in the Python version it causes the people variable to
hold a *reference* to the list. A great deal of simplification
follows on from that.

Or take another example, adapted from 'Programming Perl' (I know
my use of printf here isn't entirely idiomatic in Perl, but I'm
trying to make that aspect look as much like Python in order to
highlight the actual comparison I'm making):

    foreach $family (keys %TV) {
        printf "Family: %s", $family;
        foreach $who (@{$TV{$family}{'members'}}) {
            printf "%s (%s)\n", $who->{'name'}, $who->{'role'};

I found it much easier to explain to my less-experienced
colleagues the Python version below:

    for family in TV.keys():
        print "Family: %s" % family
        for who in TV[family]['members']:
            print "%s (%s)" % (who['name'], who['role'])

Neither "who" or "TV[family]['members']" needed to be explicitly

I also found Python's handling of OO more elegant and explicable:

    class MyClass(AnotherClass):

        def __init__(self, name):
            self.name = name

        def greet(self):
            print "Hi, I'm", self.name

    m = MyClass('Bob')

compared to this:

    package MyClass;
    @ISA = ('AnotherClass');

    sub new {
       ($class, $name) = @_;
       $self = {};
       bless $self, $class;
       $self->{'name'} = $name;
       return $self;

    sub greet {
        ($self) = @_;
        print "Hi, I'm ", $self->{'name'}, "\n";

    $a = MyClass->new('Bob');

Once I switched to Python I found other gems in the everything-
is-an-object philosophy: even modules (packages in Perl) and
classes are objects, so you can do things like this:

        import optional_fast_native_stuffmodule
        stuffmodule = optional_fast_native_stuffmodule
    except ImportError:
        import standard_slow_portable_stuffmodule
        stuffmodule = standard_slow_portable_stuffmodule


So while there may not be many things that you can do in Python
can't in Perl (but treating classes and modules as objects and
reflection would be among them), I found that the real advantage
of Python is that most things will be simpler and more
understandable. This makes it so much easier when working with
others or when revisiting my code months later.

Hamish Lawson

* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!

More information about the Python-list mailing list