Python Forum
Question about an infinite loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about an infinite loop
#4
(Mar-04-2018, 06:24 AM)Gribouillis Wrote: The return value of the main function is not the same thing as the program's exit status (because main() is not special) unless you call sys.exit(main()) as wavic does. The program should exit with value 0 to indicate success.


I need to ask everyone to forgive my ignorance, but I'm not quite understanding exactly how these programs are run.

My IDE creates the following template automatically when I start a new python project.

def main(args):
    return 0

if __name__ == '__main__':
    main()
Now this is going to really sound stupid, but my question is, "What the heck is this thing?"

Is this a class? If so does it have a name? Obviously it's a file and it has a file name. So what is the file name? The name of the program? The name of the class? The name of a module?

I'm not clear on the difference between these things.

I've learned that I can change the name of the main method as long as I change it both in the method definition as well as in the if statement at the bottom like so:

None the less it appears that Python is looking for something named '__main__' in order to run at all.

def my_method():
    return 0
 
if __name__ == '__main__':
    my_method()
Now in terms of what wavic is doing I have some questions:

Here's wavic's original code
def main():
    return 0 # it's not necessary but you can do it
 
if __name__ == '__main__':
    import sys
    sys.exit(main())
If I were to modify this code as follows it should run just fine, right?

def my_method():
    return 0 # it's not necessary but you can do it
 
if __name__ == '__main__':
    import sys
    sys.exit(my_method())
However if I were to modify this code as follows it should never run at all, right?

def main():
    return 0 # it's not necessary but you can do it
 
if __name__ == '__main__':
    import sys
    sys.exit()
I'm guessing that sys.exit() will only exit after it has run whatever is placed inside the brackets?

In other words sys.exit(method_x()) will first run method_x() and then exit the program. Is this correct?

And if so, how does sys.exit(method_x()) know when method_x() is finished running if method_x() doesn't have a return?

Sorry for the confusion here, but I'm trying to understand exactly what's going on.
Reply


Messages In This Thread
Question about an infinite loop - by Robo_Pi - Mar-04-2018, 04:45 AM
RE: Question about an infinite loop - by wavic - Mar-04-2018, 05:19 AM
RE: Question about an infinite loop - by Robo_Pi - Mar-04-2018, 05:44 PM
RE: Question about an infinite loop - by wavic - Mar-04-2018, 06:45 PM
RE: Question about an infinite loop - by Robo_Pi - Mar-04-2018, 08:42 PM
RE: Question about an infinite loop - by wavic - Mar-05-2018, 05:46 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Shocked Why this code does not go into an infinite loop? 2367409125 2 949 Dec-02-2022, 08:22 PM
Last Post: deanhystad
  Need help with infinite loop & making hotkeys/shortcuts Graxum 1 1,269 Aug-22-2022, 02:57 AM
Last Post: deanhystad
  A question about 'Event loop is closed' fc5igm 2 2,363 Oct-05-2021, 02:00 AM
Last Post: fc5igm
Exclamation question about input, while loop, then print jamie_01 5 2,800 Sep-30-2021, 12:46 PM
Last Post: Underscore
  Infinite loop problem Zirconyl 5 3,095 Nov-16-2020, 09:06 AM
Last Post: DeaD_EyE
  for loop question KEYS 1 1,799 Oct-27-2020, 11:42 PM
Last Post: jefsummers
  Netmiko Loop question sc00ter 2 3,418 Oct-24-2020, 10:54 PM
Last Post: sc00ter
  using 'while loop' output going into infinite loop... amitkb 2 2,048 Oct-05-2020, 09:18 PM
Last Post: micseydel
  while loop question KEYS 2 2,100 Sep-26-2020, 11:02 PM
Last Post: KEYS
  New to programming, loop question tomyan 1 1,728 Sep-25-2020, 04:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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