Calling functions before that are def'ed

C

Charles Larry

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
 
M

Michael Geary

Charles said:
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
 
D

Daniel Dittmar

Charles said:
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
 
P

Peter Hansen

Daniel said:
#!/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
 

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

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top