By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,163 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,163 IT Pros & Developers. It's quick & easy.

Calling functions before that are def'ed

P: n/a
Is there a way to define functions after the main part of a Python
script?

Example:

#!/usr/local/bin/python

# this code yields a NameError

print_message("hello world")

def print_message(msg):
print msg
I know functions can be put in separate files and imported, but I want
everything in a single file. Am I stuck with defining all the
functions first? If so, how come at compile-time Python can't look
ahead in the program and discover the existence of functions referred
to but not yet defined?

Regards,
Charles
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Charles Larry wrote:
Is there a way to define functions after the main part of a Python
script?

Example:

#!/usr/local/bin/python

# this code yields a NameError

print_message("hello world")

def print_message(msg):
print msg

I know functions can be put in separate files and imported, but I want
everything in a single file. Am I stuck with defining all the
functions first? If so, how come at compile-time Python can't look
ahead in the program and discover the existence of functions referred
to but not yet defined?


Function definitions are executable statements, so like any other statement
they are executed in the order they are encountered.

If you prefer the style of having functions after your main program, simply
define a main function at the top of your script, and call it at the end.

-Mike
Jul 18 '05 #2

P: n/a
Charles Larry wrote:
Is there a way to define functions after the main part of a Python
script?

Example:

#!/usr/local/bin/python

# this code yields a NameError

print_message("hello world")

def print_message(msg):
print msg


#!/usr/local/bin/python

def main ():
print_message("hello world")

def print_message(msg):
print msg

if __name__ == "__main__":
main ()

def statements are really executable statements and not declarations, so
the order is important.

Daniel

Jul 18 '05 #3

P: n/a
Daniel Dittmar wrote:

Charles Larry wrote:
Is there a way to define functions after the main part of a Python
script?

Example:

#!/usr/local/bin/python

# this code yields a NameError

print_message("hello world")

def print_message(msg):
print msg


#!/usr/local/bin/python

def main ():
print_message("hello world")

def print_message(msg):
print msg

if __name__ == "__main__":
main ()

def statements are really executable statements and not declarations, so
the order is important.


Note that the above slightly changes the behaviour of the code when globals
are involved. Putting the original code directly under the __main__ part
at the end, however, does not. (This example is too simple to exhibit any
difference in behaviour, but the following is the "safer" method for the
unwary newbie, even if it's not as clean in the long run.)

def print_message(msg):
print msg

if __name__ == "__main__":
print_message("hello world")

-Peter
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.