make order of function definitions irrelevant

J

John Roth

Joerg Schuster said:
Hello,

according to

http://mail.python.org/pipermail/tutor/2001-July/007246.html

the order of function definitions does matter in python. Does anyone
know a trick to avoid this? Is there a way to "declare" functions
without defining them?

(Making the order of function definitions irrelevant would be useful
for automatically generated python scripts.)

What you're seeing is the order of execution as a module is
loaded. As far as I can tell, that's not going to change, and I don't
see any reason to change it.

If you would tell us a bit about the application where you see
a need for this, maybe we could make some suggestions.

John Roth
 
P

Peter Otten

Joerg said:
according to

http://mail.python.org/pipermail/tutor/2001-July/007246.html

the order of function definitions does matter in python. Does anyone
know a trick to avoid this? Is there a way to "declare" functions
without defining them?

(Making the order of function definitions irrelevant would be useful
for automatically generated python scripts.)

You cannot *call* a function before it is defined:

fun()
def fun(): pass

will choke. But there is no problem with

def first(): second()
def second(): pass

So I cannot see where this could be an obstacle to script generation.
Could you provide an example?

Peter
 
P

Peter Hansen

Joerg said:
according to

http://mail.python.org/pipermail/tutor/2001-July/007246.html

the order of function definitions does matter in python. Does anyone
know a trick to avoid this?
No.


Is there a way to "declare" functions without defining them?
No.

(Making the order of function definitions irrelevant would be useful
for automatically generated python scripts.)

No, it would not.

Basically, the problem you believe is here is not a problem
in actual practice. You could add declarations to Python-the-language,
but they're irrelevant until the function is actually called.
When the function is called, it has to have been defined (not just
declared) and so the declaration would be completely redundant.

-Peter
 
A

anton muhin

Joerg said:
Hello,

according to

http://mail.python.org/pipermail/tutor/2001-July/007246.html

the order of function definitions does matter in python. Does anyone
know a trick to avoid this? Is there a way to "declare" functions
without defining them?

(Making the order of function definitions irrelevant would be useful
for automatically generated python scripts.)


Jörg
As was said, you usually don't need such a thing. If you desperatly
looking for it, something like this might work:

func = None

def caller():
assert func, 'func is None'
return func()

def foo():
return 'foo'

def bar():
return 'bar'

func = foo

print func()

Of course, in this case you'd better pass an actual function as a
parameter. But, again, almost for sureit's a flaw in your design.

regards,
anton.
 
P

Peter Hansen

anton said:
As was said, you usually don't need such a thing. If you desperatly
looking for it, something like this might work:

func = None

def caller():
assert func, 'func is None'
return func()

def foo():
return 'foo'

def bar():
return 'bar'

func = foo

print func()

Of course, in this case you'd better pass an actual function as a
parameter. But, again, almost for sureit's a flaw in your design.

As it stands, the first line in the above code is still redundant,
and can be removed with no ill effects, as "func" is not actually
called until it is bound to a real function.

-Peter
 
A

anton muhin

Peter said:
As it stands, the first line in the above code is still redundant,
and can be removed with no ill effects, as "func" is not actually
called until it is bound to a real function.

-Peter

Sure, thanks. It just a little bit clearer.

best regards,
anton.
 
P

Peter Hansen

anton said:
Sure, thanks. It just a little bit clearer.

I would respectfully claim that, to a Python programmer, it's actually
just a little bit _less_ clear, just because of the "unnecessity" of
doing it. One might have to waste a moment to try to figure out why
it's done that way.

Only to a programmer coming from another language, one which requires
such declarations, might this seem clearer, and I'm unsure about that.

(And the sooner said programmer unlearns some things, the sooner Python
will feel more comfortable.)

All IMHO... no offense intended.

-Peter
 
J

John J. Lee

Joerg Schuster said:
the order of function definitions does matter in python. Does anyone
know a trick to avoid this? Is there a way to "declare" functions
without defining them?

(Making the order of function definitions irrelevant would be useful
for automatically generated python scripts.)

Aside from what everybody has already said, generating Python code is
almost always a "don't do that" anyway.

Most of the time, it's much better do use Python's dynamic features
instead of code generation.


John
 
J

Joerg Schuster

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 my_corpus

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.

Jörg
 
J

John Roth

Joerg Schuster said:
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 my_corpus

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__"
doSomething()

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.

John Roth
 
D

Derrick 'dman' Hudson

Joerg Schuster said:
(2) Python functions

def NP():
return sequence(det(), AP(), n())

def AP():
return sequence(plus(adv(), a())

As pointed out, this is perfectly valid python code because the AP
function (and presumably det(), adv(), n(), and a()) is defined before
NP() is called. The key isn't the order of the definitions in the
file, but the order of execution. The 'def' statement is executed
like any other, in python. The function doesn't exist until it is
defined. This is rather like C, C++, and Java, you just notice it
differently because python provides a way to attempt to call a
non-existant function. In C, etc., you must define the functions
before compilation will succeed and compiltion is a prereq for
execution.

HTH,
-D

--
\begin{humor}
Disclaimer:
If I receive a message from you, you are agreeing that:
1. I am by definition, "the intended recipient"
2. All information in the email is mine to do with as I see fit and make
such financial profit, political mileage, or good joke as it lends
itself to. In particular, I may quote it on USENET or the WWW.
3. I may take the contents as representing the views of your company.
4. This overrides any disclaimer or statement of confidentiality that may
be included on your message
\end{humor}

www: http://dman13.dyndns.org/~dman/ jabber: (e-mail address removed)
 
M

Michele Simionato

Joerg Schuster said:
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.

If you wrap all your functions in a class and make them staticmethods,
the order of definition does not matter. Is this enough for you?

Michele
 
J

Joerg Schuster

Hello Michele,

thanks for your hint. Yet, I have decided to give my program a new
design which makes some old problems disappear. (Now I am having
trouble with some new problems, of course.)

Jörg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top