473,387 Members | 1,291 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

make order of function definitions irrelevant

Hello,

according to

http://mail.python.org/pipermail/tut...ly/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
Jul 18 '05 #1
14 6259

"Joerg Schuster" <js@cis.uni-muenchen.de> wrote in message
news:cr*************@pinatubo.cis.uni-muenchen.de...
Hello,

according to

http://mail.python.org/pipermail/tut...ly/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


Jörg

Jul 18 '05 #2
Joerg Schuster wrote:
according to

http://mail.python.org/pipermail/tut...ly/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
Jul 18 '05 #3
Joerg Schuster wrote:

according to

http://mail.python.org/pipermail/tut...ly/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
Jul 18 '05 #4
Joerg Schuster wrote:
Hello,

according to

http://mail.python.org/pipermail/tut...ly/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.

Jul 18 '05 #5
anton muhin wrote:

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
Jul 18 '05 #6
Peter Hansen wrote:
anton muhin wrote:
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


Sure, thanks. It just a little bit clearer.

best regards,
anton.

Jul 18 '05 #7
anton muhin wrote:

Peter Hansen wrote:
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.


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
Jul 18 '05 #8
Joerg Schuster <js@cis.uni-muenchen.de> writes:
[...]
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
Jul 18 '05 #9
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
Jul 18 '05 #10

"Joerg Schuster" <js@cis.uni-muenchen.de> wrote in message
news:cr*************@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 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
Jörg

Jul 18 '05 #11
> That's why scripts always end with the lines:
if __name__ == "__main__"
doSomething()


Thanks for pointing this out to me.

Jörg
Jul 18 '05 #12
Joerg Schuster <js@cis.uni-muenchen.de> wrote:
(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: dm**@dman13.dyndns.org
Jul 18 '05 #13
Joerg Schuster <js@cis.uni-muenchen.de> wrote in message news:<cr*************@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.


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
Jul 18 '05 #14
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

Jul 18 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Raymond Tinsel | last post by:
Hello all, I know this must be a newbie question, but I haven't found the solution in the tutorials I have read. For a dSPACE/Controldesk application I am defining several of my own modules....
5
by: Kobu | last post by:
Does anyone know how old style function definitions differ in "behaviour" to new style function definitions? I heard the old style function definitions caused integeral promotion and floating...
8
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function...
15
by: unknownbomb | last post by:
Hey all i really hope you can help me out. Im compiling a project and im getting 2 errors which are saying local function definitions are illegal. Anyone know what this mean? And especially...
1
by: kaygee | last post by:
Hi ppl can anyone tell me what this error means? error C2601: 'meanData' : local function definitions are illegal
9
by: vpascuzzi | last post by:
Here's the deal: I've been working on this little program forever now, and can't seem to get the final little glitches out of it. I am to build a vending machine, using 2 header .h files (one...
1
by: nassegris | last post by:
Hello everyone! I'm trying to write a regular expression to capture VB6 function definitions and I'm abit stuck. The rules are: Function header: * Must contain the words SUB or FUNCTION *...
8
by: borophyll | last post by:
I don't understand the difference between these two declarations int foo(char a, char b) { ... } int foo(a, b) char a, b; {
3
by: MrHenry007 | last post by:
Hello! I'm fairly new to c++ but I have been following tutorials and have created functions before, but not one using a string. I can't work out what the problem is here. The function is supposed...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.