Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use def main()? [duplicate]

I've seen some code samples and tutorials that use

def main():     # my code here  if __name__ == "__main__":     main() 

But why? Is there any reason not do define your functions at the top of the file, then just write code under it? ie

def my_function()     # my code here  def my_function_two()     # my code here  # some code # call function # print(something) 

I just wonder if there is any rhyme to the main?

like image 456
Wizzard Avatar asked Oct 28 '10 08:10

Wizzard


People also ask

What is the use of __ main __?

__main__ is the name of the environment where top-level code is run. “Top-level code” is the first user-specified Python module that starts running. It's “top-level” because it imports all other modules that the program needs. Sometimes “top-level code” is called an entry point to the application.

Is if name == Main necessary?

The Reason Behind if __name__ == '__main__' in Python You might have seen this one before: the syntax which often gets ignored because it doesn't seem to hinder the execution of your code. It may not seem necessary, but that's only if you're working with a single Python file.

What is __ main __ PY in Python?

Python looks for a file named __main__.py to start its execution automatically. If it doesn't find it it will throw an error else it will execute main.py and from the code, you can well understand that it will import the modules from src to find the area.

What is the purpose of a main loop in Python?

Mainloop in Python Tkinter is an infinite loop of the application window which runs forever so that we can see the still screen. The application window is like a frame that keeps on destroying every microsecond but the main loop keeps on creating a new updated window.


2 Answers

Without the main sentinel, the code would be executed even if the script were imported as a module.

like image 171
Ignacio Vazquez-Abrams Avatar answered Oct 02 '22 12:10

Ignacio Vazquez-Abrams


Everyone else has already answered it, but I think I still have something else to add.

Reasons to have that if statement calling main() (in no particular order):

  • Other languages (like C and Java) have a main() function that is called when the program is executed. Using this if, we can make Python behave like them, which feels more familiar for many people.

  • Code will be cleaner, easier to read, and better organized. (yeah, I know this is subjective)

  • It will be possible to import that python code as a module without nasty side-effects.

  • This means it will be possible to run tests against that code.

  • This means we can import that code into an interactive python shell and test/debug/run it.

  • Variables inside def main are local, while those outside it are global. This may introduce a few bugs and unexpected behaviors.

But, you are not required to write a main() function and call it inside an if statement.

I myself usually start writing small throwaway scripts without any kind of function. If the script grows big enough, or if I feel putting all that code inside a function will benefit me, then I refactor the code and do it. This also happens when I write bash scripts.

Even if you put code inside the main function, you are not required to write it exactly like that. A neat variation could be:

import sys  def main(argv):     # My code here     pass  if __name__ == "__main__":     main(sys.argv) 

This means you can call main() from other scripts (or interactive shell) passing custom parameters. This might be useful in unit tests, or when batch-processing. But remember that the code above will require parsing of argv, thus maybe it would be better to use a different call that pass parameters already parsed.

In an object-oriented application I've written, the code looked like this:

class MyApplication(something):     # My code here  if __name__ == "__main__":     app = MyApplication()     app.run() 

So, feel free to write the code that better suits you. :)

like image 21
Denilson Sá Maia Avatar answered Oct 02 '22 13:10

Denilson Sá Maia