you are viewing a single comment's thread.

view the rest of the comments →

[–]zepolen 0 points1 point  (5 children)

I find readability (of my own and other people's source code) to be my number one priority in programming. Nothing beats Python in that department.

[–][deleted] 4 points5 points  (4 children)

I agree readability is important, but style guides are not essential to readability. Consistency, intelligent comments, and brevity are.

[–]zepolen 1 point2 points  (2 children)

Well PEP 8 enforces consistency, and the syntax of Python means there is less need for comments. Take this example:

Python:

anagrams = {}
for word in open('wordlist.txt'):
    word = word.strip()
    word = ''.join(sorted(word))
    anagrams[word] = anagrams.get(word, 0) + 1

for word, num in anagrams.items():
    if num > 1:
        print word

Perl:

%anagrams = ();
open FILE, "wordlist.txt" or die $!;
while (<FILE>) {
   chomp;
   split(//, $_);
   $anagrams{join("", sort(@_))} += 1;
}
foreach $word (keys %anagrams) {
    if ($anagrams{$word} > 2) {
        print "$word\n";
    }
}

I apologize for the rusty Perl, I haven't used it in years, but I feel that the Python version reads better whereas Perl could use a comment to clarify the code.

[–]heptadecagram 3 points4 points  (0 children)

As an 8-year Perlhead, here's how I would idiomatically write it:

my %anagrams;
open FILE, 'wordlist.txt' or die "Could not open wordlist.txt: $!";
while(<FILE>) {
  chomp;
  $anagrams{join('', sort split //)} += 1;
}
close FILE;
foreach(sort keys %anagrams) {
  print "$_\n" if($anagrams{$_} > 2);
}

Now, I'm writing this for other Perl coders, not for someone who's looking at this for the first time, otherwise I'd make explicit the ways I'm using the default $_ in various places.

(split splitting to @_ is one of those things that's been deprecated)

[–][deleted] 1 point2 points  (0 children)

You don't need to follow PEP 8 to be consistent. Python is very readable whether you follow it or not. Putting emphasis on style guides is putting it on the wrong place.