make order of function definitions irrelevant
newsgroups at jhrothjr.com
Tue Nov 11 13:01:01 CET 2003
"Joerg Schuster" <js at cis.uni-muenchen.de> wrote in message
news:crtr80fz49w.fsf at pinatubo.cis.uni-muenchen.de...
> Thank you all, so far. I had asked the question because I am writing a
> program that translates linguistic grammars into python functions. The
> python functions are supposed to be called by another program of mine
> that does regular expression matching. Like so:
> (1) Linguistic grammar
> NP --> det AP n
> AP --> Adv+ a
> ( Lexicon is given. Like so:
> Det --> ['the', 'a', ...]
> Adv --> ['really', ...]
> A --> ['blue', 'nice', ...]
> N --> ['programmer', 'biologist', ...]
> (2) Python functions
> def NP():
> return sequence(det(), AP(), n())
> def AP():
> return sequence(plus(adv(), a())
> (3) Matching routine "pm":
> $ pm -pattern "NP_biologist VP_love NP_programming-language" -file
> Actually, (2) is lot more complex than in the example, because it needs
> "wildcards" that can be filled with semantic and other information by
> pm. Yet, the user should be able to write his own grammar, therefore
> the translation (1) -> (2). (Of course, the grammar would not be a
> context free grammar, though it looks like one.)
> *If* the order of function definitions did not matter, then the
> order of the grammar rules in (1) would not matter, either.
> Yet, I thought the translation program over, and I will probably give
> it a new design, today.
Actually, the order doesn't matter; what matters is that the
function *definition* has been installed into the namespace
before it's invoked. The function isn't executed until it's called,
and it won't be called until something that *isn't* a function
starts the program by calling something.
That's why scripts always end with the lines:
if __name__ == "__main__"
where doSomething() is the first function to
execute in the program.
Everything up to that point is usually just
loading function and class definitions into the
module name space.
You can define functions in any order you want, as long as
the invocation of the whole mess is at the end of the module.
More information about the Python-list