newbie: declare function before definition?

Alex Martelli aleax at aleax.it
Fri Apr 5 10:03:56 EST 2002


David Kramer wrote:

> Disclaimer: 16 years programming experience, 1 week experience with
> Python.
> 
> It appears to me that in Python, functions need to be defined before they
> are called (physically in the file, I mean).  This means any piddly little

No, only before in the sense of TIME.  You need to execute a def statement
for the function before you execute a call to the function, in the clock
sense of 'before'.

> utility functions have to be before my main routine, and the reader has to
> wade through them before getting to the main routine.

Not at all, as long as the 'main routine' IS a routine, i.e., a function,
not just toplevel code of course.


> In some other languages, one can declare a function, and define it later,
> and the compiler is happy.  Is there a way to do this in Python?

Sure!  Make your 'main routine' into a routine called main, def it where
you want, call it at the end.


e.g.:

def main():
    print "I'm gonna call a function just for fun"
    function()
    print "there, told ya"

def function()
    print "I'm a tiny little function..."

main()


Alex




More information about the Python-list mailing list