Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I define functions inside or outside of main()?

After reading the following, I think I understand the value of wrapping even the simplest of scripts in a main() function.

  • Should I use a main() method in a simple Python script?
  • Why use def main()?

Should I define all my functions inside or outside main()?

Is there a right or wrong way? What are the advantages and disadvantages of both approaches?

like image 566
chishaku Avatar asked Jul 31 '14 21:07

chishaku


People also ask

Should I put functions before or after main?

For a function defined in the same file where main is defined: If you define it before main , you only have to define it; you don't have to declare it and separately define it. If you define it after main , you have to put a matching prototype declaration before main .

Can I define function inside main?

The declaration of a user-defined function inside the main() function and outside main() is similar to the declaration of local and global variables, i.e. When we declare a function inside the main, it is in local scope to main() and that function can be used in main() and any function outside main() can't access the ...

Is it good practice to define a function inside a function?

It's actually fine to declare one function inside another one. This is specially useful creating decorators. However, as a rule of thumb, if the function is complex (more than 10 lines) it might be a better idea to declare it on the module level.

Should functions be inside or outside class?

Functions should be declared inside the class to bound it to the class and indicate it as it's member but they can be defined outside of the class. To define a function outside of a class, scope resolution operator :: is used.


2 Answers

I would discourage defining functions inside of main(), especially if you have multiple files in your Python script. Any function B defined inside of function A cannot be used by anything outside of function A, severely limiting its usability. Functions defined inside main() cannot be imported elsewhere, for example.

Defining functions inside of main() lets you easily override other functions you may have written elsewhere that have the same name, but the instances where this is really useful are few and far between, and you should not be doing it as a general practice. Overall there are many more reasons for defining functions outside of main() than there are for defining them inside, and if you're learning Python that's definitely how you should handle it.

like image 119
TheSoundDefense Avatar answered Sep 28 '22 04:09

TheSoundDefense


If you define a function inside of the main function, you won't be able to use it from the outside. Here is an example:

def outer():
    print "outer function" 

def main():
    def inner():
        print "inner function"
    inner()

if __name__ == "__main__":
    main()  # outputs: "inner function"
    outer() # outputs: "outer function"
    inner() # fails!
like image 23
miindlek Avatar answered Sep 28 '22 04:09

miindlek