views:

127

answers:

4

Greetings StackOverflow,

Is it possible to call a function without first fully defining it? When attempting, i get the error: "*function_name* is not defined". I am coming from a c++ background so this issue stumps me.

Declaring the function before works:

def Kerma():
        return "energy / mass"    

print Kerma()

However, attempting to call the function without first defining it gives trouble:

print Kerma()

def Kerma():
    return "energy / mass"

In c++, you can declare a function after the call once you place its header before it.

Am I missing something here? ~ The Gentle One.

+2  A: 

This isn't possible in Python, but quite frankly you will soon find you don't need it at all. The Pythonic way to write code is to divide your program into modules that define classes and functions, and a single "main module" that imports all the others and runs.

For simple throw-away scripts get used to placing the "executable portion" at the end, or better yet, learn to use an interactive Python shell.

Eli Bendersky
+5  A: 

One way that is sort of idiomatic in Python is writing:

def main():
    print Kerma()

def Kerma():
    return "energy / mass"    

if __name__ == '__main__':
    main()

This allows you to write you code in the order you like as long as you keep calling the function main at the end.

Muhammad Alkarouri
@Muhammad: while this is a viable approach, it surely isn't idiomatic in Python. Quite the opposite, you'll notice most `main` functions are usually placed at the end.
Eli Bendersky
@Eli Bendersky: I'd submit that the `if __name__ == '__main__':` switch is the common idiomatic part.
S.Lott
@Eli: the idiomatic part is the if clause at the end: no code at the top level of the module, then at the end invoke a main function if the module is __main__.
Ned Batchelder
@S.Lott and @Ned: of course, but note Muhammad's sentence after the code sample which specifically points to placing `main` above and calling it below. The `if __name__` part is tangential in that answer, or at least not given attention.
Eli Bendersky
@Eli: The idiomatic part is the `if ... __main__':`. I didn't say that the `main` should be placed above the rest of the code. I said the important part is calling `_\_main__` at the end, and it doesn't matter where you place its definition then.
Muhammad Alkarouri
@Muhammad: alright then :-)
Eli Bendersky
+1  A: 

Python is a dynamic languuage and the interpreter always takes the state of the variables (functions,...) as they are at the moment of calling them. You could even redefine the functions in some if-blocks and call them each time differently. That's why you have to define them before calling them.

eumiro
A: 

When a Python module (file) is run, the top level statements in it are executed in the order they appear from top to bottom (or beginning to end). This mean you can't reference a variable until you've defined it. For this reason the following will generate an error:

c = a + b
a = 13
b = 17

Likewise function 'def' and 'class' statements are also executable in Python unlike in some other languages, so you can reference a function until it is defined, which is why you first example doesn't work -- you're referencing the Kerma() functin before the 'def' statement and its body have been executed, so it's not defined yet at that point in the program.

Programs in languages like C++ are preprocessed before being run and during the compilation process the program is read several in and processed all at once. The language also features declaration statements which allow the name and calling sequence of functions to be defined before the body so when their name are encountered the compiler know enough information to compile a call to them even though they don't exist yet.

martineau