Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make function definition in a python file order independent

Tags:

python

I use Python CGI. I cannot call a function before it is defined.

In Oracle PL/SQL there was this trick of "forward declaration": naming all the functions on top so the order of defining doesn't matter.

Is there such a trick in Python as well?

example:

def do_something(ds_parameter):     helper_function(ds_parameter)     ....  def helper_function(hf_parameter):     ....  def main():     do_something(my_value)  main() 

David is right, my example is wrong. What about:

<start of cgi-script>  def do_something(ds_parameter):     helper_function(ds_parameter)      ....   def print_something():      do_something(my_value)   print_something()   def helper_function(hf_parameter):      ....   def main()     ....  main() 

Can I "forward declare" the functions at the top of the script?

like image 257
user37986 Avatar asked Apr 16 '09 21:04

user37986


People also ask

Do Python functions have to be defined in order?

All functions must be defined before any are used. However, the functions can be defined in any order, as long as all are defined before any executable code uses a function.

Do you have to define functions first in Python?

Python is a dynamic programming language 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.

Do functions need to be at the top in Python?

Although in Python you can call the function at the bottom of your program and it will run (as we have done in the examples above), many programming languages (like C++ and Java) require a main function in order to execute.


2 Answers

All functions must be defined before any are used.

However, the functions can be defined in any order, as long as all are defined before any executable code uses a function.

You don't need "forward declaration" because all declarations are completely independent of each other. As long as all declarations come before all executable code.

Are you having a problem? If so, please post the code that doesn't work.


In your example, print_something() is out of place.

The rule: All functions must be defined before any code that does real work

Therefore, put all the statements that do work last.

like image 184
S.Lott Avatar answered Sep 23 '22 19:09

S.Lott


An even better illustration of your point would be:

def main():     print_something()      ....  def do_something(ds_parameter):     helper_function(ds_parameter)      ....   def print_something():      do_something(my_value)    def helper_function(hf_parameter):      ....    main() 

In other words, you can keep your definition of main() at the top, for editing convenience -- avoiding frequent scrolling, if most of the time is spent editing main.

like image 32
Stan Vernon Avatar answered Sep 19 '22 19:09

Stan Vernon