Function declarations

Makhno mak at imakhno.freeserve.co.uk
Mon Nov 20 07:55:34 EST 2000


>The usual approach is to put all of your other top-level code inside of
>a function so that the only executable statements are variable and function
>definitions.  This function is often called 'main' and run by the last
>bit of code in the file, as in:

I soon intend to write a GUI management library in Python that I already
have operating in Perl.
User write a script which constructs the GUI, and controls how how it
behaves (no surprises there).
In Perl, the usual procedure is (in this order):

#GUI construction code (using references to 'callback functions' below
#enter callback here
#define callback functions

This will not work in Python, because the callback functions will not be
defined when entering callback.
I can change it to

#GUI construction code (using references to 'callback functions' below
#define callback functions
#enter callback here

However, this also does not help because now I need to take a reference to a
function that has not yet been defined. The only way I can get it to work is
to write

#define callback functions
#GUI construction code (using references to 'callback functions' below
#enter callback here

However, I believe this it is confusing unless I put the GUI construction
(afterall, that runs first, then the callbacks are called) but I can't
construct them without setting callback, and I can't set callbacks with
defining the callbacks first....


The only thing I can think of is to write a wrapper around the GUI-creation
code, like this:

def Init():
    #GUI construction code (using references to 'callback functions' below
#define callback functions
Init()
#enter callback here


Perhaps with some refinement (eg Maybe the callback() subroutine call can
take Init() as an argument, rather than have the user call it himself) this
is the best solution I can come up with. I preserve GUI creation-order
without having to impossibly pre-declare functions.
I was hoping that Python provided a better way to manipulate callbacks in
this manner.







More information about the Python-list mailing list