Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare function at end of file in Python

Is it possible to call a function without first fully defining it? When attempting this 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?

like image 701
TheGentleOne Avatar asked Sep 20 '10 18:09

TheGentleOne


People also ask

Can you define a function after you call it Python?

No! It is not possible. Python does not allow calling of a function before declaring it like C. This is possible in some languages like JavaScript but not in Python.

How does Python handle end of file?

Use Walrus Operator to Find End of File in Python It is denoted by := . This operator is basically an assignment operator which is used to assign True values and then immediately print them.

Can we define function anywhere in Python?

In C++, you can declare a function after the call once you place its header before it. Am I missing something here? In Python there's no "declare". There's the definition (which must be complete) or there's nothing.


2 Answers

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.

like image 192
Muhammad Alkarouri Avatar answered Sep 20 '22 17:09

Muhammad Alkarouri


When a Python module (.py file) is run, the top level statements in it are executed in the order they appear, from top to bottom (beginning to end). This means you can't reference something until you've defined it. For example the following will generate the error shown:

c = a + b  # -> NameError: name 'a' is not defined a = 13 b = 17 

Unlike with many other languages, def and class statements are executable in Python—not just declarative—so you can't reference either a or b until that happens and they're defined. This is why your first example has trouble—you're referencing the Kerma() function before its def statement has executed and body have been processed and the resulting function object bound to the function's name, so it's not defined at that point in the script.

Programs in languages like C++ are usually preprocessed before being run and during this compilation stage the entire program and any #include files it refers to are read and processed all at once. Unlike Python, this language features declarative statements which allow the name and calling sequence of functions (or static type of variables) to be declared (but not defined), before use so that when the compiler encounters their name it has enough information to check their usage, which primarily entails type checking and type conversions, none of which requires their actual contents or code bodies to have been defined yet.

like image 37
martineau Avatar answered Sep 23 '22 17:09

martineau