Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2010/09/09

Question to Python Programmers

I am not a Python guy. I'm a Perl guy. I've tried it several times, and each time, I've found something that offended me about it. The first time, yes, was about whitespace. Finding the one SpaceTab intent in a page full of Tab indents took an hour and soured me. But the second time, the way Python's equivalent to LWP worked was seriously counterintuitive.

I've found something exciting and new that bugs me. Take this as a sample chunk of perl.

use This ;
use That ;
use The::Other::Thing ;

a() ;
for my $i ( b() ) {
    c($i)
    }

sub a {
    ...
    }

sub b {
    ...
    }

sub c {
    ...
    }

I edit a program and, after the declarations, all my central logic is up top. Compare a Python program.

import this
import that
import the_other_thing

def a:
    ...

def b:
    ...

def c:
    ...

a()
b = b()
for i in b:
    c(i)

This puts the "main" of the program at the bottom. I couldn't even fake it and make a def main(): and put it somewhere, because it would have to come after all the functions it uses anyway, so it would be useless.

If programs are to be written for other programmers to read and only incidentally for computers to run, it seems like this forced formatting seems wrong. But I'm willing to be taught. Am I missing something on this one?

4 comments:

  1. Assuming spaces where they have been stripped by the comment editor:

    def Main():
    A()
    B()

    def A():
    pass

    def B():
    pass

    if __name__ == '__main__':
    Main()

    ReplyDelete
  2. Thanks for that. My eyes are refusing to focus on my Sikuli->Jython code right now, but that's something I'm going to have to work in.

    ReplyDelete
  3. I mean no offense, but if the whitespace issue put you off that much, then I perceive that you are just looking for a reason to dislike Python.

    I fairly leaped for joy when I first read of how Python could free us from the tyranny of punctuation overload and all those endless ugly curly braces.

    Just as an exercise, count the number of punctuation characters in your two examples in the article. And ask: does the extra punctuation in the Perl code actually accomplish *anything* or is it really just optic-nerve noise.

    Then imagine if we were comparing a similar snippet of Python to C++.

    ReplyDelete
  4. rottweiler,

    My first experience with Python was a piece of sample code sent to me by someone in my LUG, abut a decade ago. Imagine yourself coding and debugging and not solving something, until someone looks over your shoulder and says "Isn't that supposed to be a comma?" Most everybody has had an issue that they just couldn't see. Modern editors with syntax checking help with that, but if you have no syntax checking in your editor (like I did not have a decade ago, the first time I tried Python) and you don't have an editor that automagically turns your tabs into spaces (like I did not have a decade ago, the first time I tried Python), there's little to tell you what the problem is. I was given what was claimed to be working example code in a language I did not understand and spent an hour trying to figure out why it didn't work, so my initial attempt was poisoned.

    I studied journalism before computer science, and learning to do page layout taught me the importance of white space, and I do tend to do plenty of indenting in my code. I just object to having something I can't see have a chance of breaking my code. But, while the initial distaste still lingers, I have tried a few other times, each time finding that I still feel is wrong, or at least something that I like the Perl way better. Honestly, the thing that sells me on Perl isn't so much the syntax, although I've come to love it, as the enormous warehouse of previously-invented wheels known as CPAN.

    Right now, I'm coding Python because there's a windowed app whose API I have no access to, so I need to use Sikuli to automate the GUI. Sikuli is in Java with Jython as the scripting language, so to make this work, I must learn to write Python. I don't like it, but I like the paycheck so I must do it, and it is best to learn to do it the best way possible. The level of my Python code was much better at 5pm today than it was at 8am when I started, in part because of Geoff's response.

    (There are also a few web-app APIs I'm playing with that have much better sample implementations in Python than in Perl, but I'm not so much getting paid for that stuff.)

    Re: your challenge, it tells the reader something, and programs are to be read and only incidentally to be run. It reinforces the indenting to group things. Remember the old saw about giving presentations: you tell 'em what you're gonna tell 'em, you tell 'em, then you tell 'em what you told 'em. Repetition and emphasis of important points is useful, and being concise is not always helpful. Would you prefer a config file filled with abbreviations over one that uses the verbose version?

    I am aware that this is my opinion and my bigotry. I try to be up front about these things so that people can filter my comments though them and more easily distinguish the truth and the opinion.

    Still, thanks for the comment.

    ReplyDelete