Calling functions before that are def'ed

Charles Larry allmyjunkemails at yahoo.com
Mon Sep 22 17:18:58 EDT 2003


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




More information about the Python-list mailing list