Why should I switch to Python?

Thomas Wouters thomas at xs4all.net
Thu May 11 16:08:38 CEST 2000

On Thu, May 11, 2000 at 12:09:07AM -0500, James Felix Black wrote:
> > Two big ones come to mind: using complex data structures, and
> > modularizing your code. Both are much easier in python.

> I won't dispute your second point, but I don't understand your first.

I do. I'm trying to grok a colleagues' OO-perl code, and it has me dazzled.
Not in how it works or what it does, but *why*. It seems to be a strange mix
of normal classes and metaclasses, using hashes and identifier strings and
indexes into arrays -- I have a nagging suspision this 1000+ lines Perl
blob could be done in less than 200 lines Python, using regular classes. I
wont know for sure until said colleague returns from his week off to France,
partly because he took the Perl OO book with him, and I haven't read it yet,
and partly because I'm not sure the code isn't intended to do more than
implement generic classes with a bit of metaclass flavour.

Perhaps I'll understand it better once I learn exactly how to work with OO
perl -- writing it, I mean, instead of using it. And this is exactly the
problem I have with Perl, compared to Python. In Python, if you can write a
script, you can write a module. Move your functions & classes to a different
file, or wrap your executing code in a 'if __name__ == '__main__':' block,
and you have a module.

In Perl, if you want anything more complicated than a package with
subroutines (say, classes), you are entering a world of hurt. (Or at least I
was ;)

> > In perl, doing this is so painful to me that I'm not even going to
> > try it now, and it only gets worse as your data structures get more
> > complicated.
> I'm not sure exactly what you mean:
> %friends = { Bob => [ ], Jane => [ "Lisa", "Mabel", "Freddy" ],
>              Lisa => [ "Mabel" ] };
> for $name (keys %friends) {
>   print "$name has these friends:\n";
>   for $f (@{ $friends{ $name } }) {
>     print " ", $f;
>   }
>   print "\n";
> }

Actually, the initialization of your hash is wrong! Using perl -w you'll see
this error:

Reference found where even-sized list expected at - line 1.

Now guess what's wrong :-) (hint: literal dictionaries dont exist in perl,
as such; you have to use lists. This is why 'reverse %hash' works as a
key<->value swap ;-)

I didn't notice this when reading the code, though, and I have to admit I
was kind of surprised to learn HASH(0x80e54e0) didn't have any friends ;)

As for the second part, for more Perl adventure, you can do it like this:

for my $name (keys %friends) {
    print "$name has these friends:\n";
    print join(' ', @{ $friends{ $name } }) . "\n";

or even this:

map {
   print "$_ has these friends:\n" . join(' ', @{ $friends{ $_ } }) . "\n"
} keys %friends;

Not that I used perl map much until I learned Python, though.

> What I (as a perl programmer/python novice) really miss in python are 
> true closures.  Is this a language feature yet?  One marvelous thing 
> that python has, though, is reduce, which I find so massively useful 
> that I carry it around with me in perl land wherever I go.

Well, you can more-or-less fake closures (enough to be functional, in any
case) using lambda:

>>> import string
>>> l = ["spam", "ham", "eggs"]
>>> characters = [", spam, ", " ", ", ham, ", ", eggs, "]
>>> map(lambda x, l=l, s=string: s.join(l, x), characters)
['spam, spam, ham, spam, eggs', 'spam ham eggs', 'spam, ham, ham, ham,
eggs', 'spam, eggs, ham, eggs, eggs']

Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!

More information about the Python-list mailing list