I'm doing a lot of work in Python these days, and while I like a lot of things about the language, and have even gotten over the things that initially bothered me, there are some structural things that make little sense to me. Autovivification (or more specifically, the lack of it) is high on that list.
In Perl, when you want to store the number of bytes of traffic that have been seen in a log file for a specific system on a specific day, you do something like this:
$logdata{$host}{$day}{bytes} += $number_of_bytes;
Nice and simple. In Python, that's:
logdata.setdefault(host,{})
logdata[host].setdefault(day,{})
logdata[host][day].setdefault('bytes',0)
logdata[host][day]['bytes'] += number_of_bytes
# UPDATE: note that this can be simplified to:
h = logdata.setdefault(host,{})
d = h.setdefault(day,{})
d.setdefault('bytes',0)
d['bytes'] += number_of_bytes
# It's still annoying, but certainly shorter.
The other item that comes up, here, is auto-quoting. I really miss being able to say:
$foo{bar}
rather than
foo['bar']
I don't know why I find quotes to be so cumbersome when I don't find brackets to be. It's just a feeling that I'm doing a lot of extra work to say, "this is the thing I wanted to look up." Then again, quoting in Perl is vastly superior on so many levels that it's a rather moot point. From the balanced quoting operators:
$pseudo-text = q{/\/\ & /\/\s};
$python = qq{a = { 'a':1 }
b = """Another '''day'''\nAnother \$"""};
$perl = q{$c = qq{Hello, world\n}};
to its powerful combination of regular expression text and code:
s{ perl \s* \{ (.*) \} }{ $1 }eesx;
Again, I love Python. It has a great object system and much cleaner function declaration syntax than Perl. I just wish it weren't so annoying sometimes (a feeling that I'm sure Python programmers have when using Perl). Perhaps someday, those of us who enjoy both languages will get together and write a new one. Maybe we'll call it Ruby. ;-)









Leave a comment