Function definition

forman.simon at gmail.com forman.simon at gmail.com
Mon Jun 19 21:57:07 EDT 2006


aarondesk wrote:
...
>
> Now I've tried putting the function declaration after the call but the
> program wouldn't work. Is there anyway to put function declarations at
> the end of the program, rather than putting them at the beginning,
> which is rather clunky?
>
> Thanks.
> Aaron

A function can call functions defined "below" it:

def a():
    return b()

def b():
    return 'foo'

print a() # prints 'foo'

because at runtime both functions exist in the current namespace.  But
module-level statements can't call functions defined below them because
the function objects haven't been created yet.  That's why you get a
NameError, the name 'b' won't been bound to anything until after the
def statement has been interpreted.

You can, of course, put your functions in another module.

Hope that helps,
~Simon




More information about the Python-list mailing list