Python Forum
I don't get the order of how things are executed with main() - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: I don't get the order of how things are executed with main() (/thread-28476.html)



I don't get the order of how things are executed with main() - pythonrookie - Jul-20-2020

Could someone please explain the order of the execution below? I thought main() is the very first function that get executed but somehow the print statement indicates main() cames at the end.

def main():
    print("This is main")


class user_input():
    print("This is class")

print(user_input())    


if __name__ == "__main__":
    main()
Quote:This is class
<__main__.user_input object at 0x04A26328>
This is main



RE: I don't get the order of how things are executed with main() - Yoriz - Jul-20-2020

def main(): # this is first, it is defined but doesn't do anything until called
    print("This is main") # this is eight
 
 
class user_input(): # this is second, it is defined but doesn't do anything until called
    print("This is class") this is fourth
 
print(user_input()) # this is third and fifth and actions straight away as its a statement and a function call in the global space
 
 
if __name__ == "__main__": # this is sixth and actions straight away as its a conditional statement in the global space
    main() # this is seventh and actions straight away as its a function call in the global space

The above is actually wrong, the print("This is class") happens when the class is defined so is actually third
You can use this link to visualise code execution


RE: I don't get the order of how things are executed with main() - deanhystad - Jul-20-2020

Functions execute in the order they are called. Code executes from top to bottom (unless changed by a loop or an if statement).

In your program the first statement in the program that can run is
print(user_input())
the second statement is
if __name__ == '__main__':
    main()



RE: I don't get the order of how things are executed with main() - pythonrookie - Jul-21-2020

Thank you very much for the answer deanhystad/Yoriz.

I thought main() is always the very first function to get executed when the script is run directly (not imported) which is the feature of "if __name__ == '__main__':" statement.


RE: I don't get the order of how things are executed with main() - deanhystad - Jul-21-2020

The purpose of "if __name__ == "__main__:" is to prevent executing statements when the file is imported. If I wanted to use your user_input class I could import your module. When I do this I would not want to run your main() function, and the if __name__ == "__main__" prevents that from happening because your file is no longer the "__main__" file.

As for "main()" always being the first function to get executed, there is nothing special about using "main()" as a function name other than convention. You could call your main function "doit()" or something else, or you could not even have a main function. For example, your module would work exactly the same written this way:
class user_input():
    print("This is class")

print(user_input())    

if __name__ == "__main__":
     print("This is main")



RE: I don't get the order of how things are executed with main() - pythonrookie - Jul-21-2020

Thank you deanhystad