Cookie Notice

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


Python Grrr

As mentioned earlier, I'm trying to code Sikuli, which is a tool for scripting GUI events. Sikuli uses Python, or more accurately, Jython, as the scripting syntax. I have tried to get beyond print "Hello World" in Python a few times, and have largely failed. It's simple inline stuff so far, so not much problem. But I'm wanting to group. Consider this Perl code:

use subs qw{ myFunction } ;
myFunction('x') ;

sub myFunction {
    my $x = shift @_ ;
    print qq{$x\n} ;

I open the file in the editor and my function is at the bottom and my logic is on top. This makes it easier to understand the code when it's more than one line of logic and one function.

def myFunction(x):
    print x 


This is functionally the same. Some might look at the syntax and prefer it. Fine. But if you put myFunction() before def myFunction():, you have an undefined function and an error. Which means you 1) get your logic at the bottom or 2) put your functions into a module, or whatever the term of art is in Python. I'm writing Sikuli, so I don't know exactly how many Python trics I can really do.

So, is that it? Must I put my logic at the bottom?

1 comment:

  1. Afraid so.

    In python, "def func(x): do stuff" behaves mostly at runtime; it's more like the following in perl:

    *func = sub {
    my ($x) = @_;
    print "$x\n;