Python Forum
I don't get the order of how things are executed with main()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I don't get the order of how things are executed with main()
#1
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
Reply
#2
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
Reply
#3
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()
Reply
#4
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.
Reply
#5
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")
Reply
#6
Thank you deanhystad
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to not open the context menu if a mouse gesture is executed? MicheliBarcello 2 661 Aug-22-2023, 02:47 PM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,277 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  code not working when executed from flask app ThomasDC 1 878 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  Can a variable equal 2 things? Extra 4 1,484 Jan-18-2022, 09:21 PM
Last Post: Extra
  Unusual things to do with unittest/HTMLTestRunner AndyHolyer 0 2,141 Jul-29-2020, 02:43 PM
Last Post: AndyHolyer
  Script works when executed from command prompt but not when executed in SDP Tippex 0 2,000 Apr-07-2020, 04:26 PM
Last Post: Tippex
  how to get PID's of linux commands executed through python modules? Manikandan_PS 4 3,045 Mar-12-2020, 07:16 AM
Last Post: Manikandan_PS
  Can a module be executed even if the computer running it does not install it? CFYB 5 3,405 Feb-08-2020, 01:56 PM
Last Post: snippsat
  C API: Writing Executed Code to a File myanrueller 0 1,701 Nov-17-2019, 09:35 PM
Last Post: myanrueller
  Urls in a file to be executed pyseeker 2 2,034 Sep-09-2019, 03:38 PM
Last Post: pyseeker

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020