Python Forum
Question about an infinite loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about an infinite loop
#1
If I have a program that loops infinitely, should I place a return at the end of that program anyway?

Example:
def main():
    while (True):
        code block
    return 0

if __name__ == '__main__':
    main()
It should never execute that return but the text editor in my IDE seems to like that return to be there so it has some idea where the end of the program should be. Big Grin

I can't imagine that it hurts anything to leave that redundant return in the code?
Reply
#2
Probably this is the way. Lets professionals say something. I code for fun.

def main():
    return 0 # it's not necessary but you can do it

if __name__ == '__main__':
    import sys
    sys.exit(main())
Notice that in Windows the return code for a successful run program is 1. As I remember.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
(Mar-04-2018, 04:45 AM)Robo_Pi Wrote: I can't imagine that it hurts anything to leave that redundant return in the code?
A main() function is not special in a python program, it is an ordinary function. A program doesn't need a main function. When there is no return statement in a function, it returns None (if it ever returns). It means that a return statement is never necessary. In your case, the return statement is useless because it is never executed.

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.
Reply
#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
#5
(Mar-04-2018, 05:44 PM)Robo_Pi Wrote:
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?

This will run just fine.

When the python code runs as a program "__main__" string is assigned to the __name__ special variable.
If the same code is imported as a module, __name__ will contain the name of the module. Any .py file can be imported as a module.

The if __name__ == "__main__": statement is a convention in the Python coding. It checks if the file is running as a program, not imported as a module and if that is the case the code block is executed.
This is why the most of the python programmers are defining a main() function so if someone else looks at the code he/she knows what it contains. The code to run the file as a program.

Because this became almost standard in the Python coding most of the IDE programs generate it as a template.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
Is Python some sort of an interpreter?

Do you need to have Python on a machine in order to run a Python program?

And does Python then actually read the source code and interpret that into machine instructions?

I'm asking because I think C is a compiler. Once you compile the code to a *.exe file you no longer need to have the C language on the computer. The program has now been complied into machine executable code that the CPU can process directly.

Is this correct?
Reply
#7
Yes, you must have Python installed.

You can use the interpreter or you can make an .exe file. Python is a general-purpose programming language. It can be used for nearly everything.
Of course, if you make an exe you can run it on a machine without Python installed just like any other program.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Shocked Why this code does not go into an infinite loop? 2367409125 2 835 Dec-02-2022, 08:22 PM
Last Post: deanhystad
  Need help with infinite loop & making hotkeys/shortcuts Graxum 1 1,124 Aug-22-2022, 02:57 AM
Last Post: deanhystad
  A question about 'Event loop is closed' fc5igm 2 2,165 Oct-05-2021, 02:00 AM
Last Post: fc5igm
Exclamation question about input, while loop, then print jamie_01 5 2,616 Sep-30-2021, 12:46 PM
Last Post: Underscore
  Infinite loop problem Zirconyl 5 2,923 Nov-16-2020, 09:06 AM
Last Post: DeaD_EyE
  for loop question KEYS 1 1,697 Oct-27-2020, 11:42 PM
Last Post: jefsummers
  Netmiko Loop question sc00ter 2 3,264 Oct-24-2020, 10:54 PM
Last Post: sc00ter
  using 'while loop' output going into infinite loop... amitkb 2 1,910 Oct-05-2020, 09:18 PM
Last Post: micseydel
  while loop question KEYS 2 1,976 Sep-26-2020, 11:02 PM
Last Post: KEYS
  New to programming, loop question tomyan 1 1,590 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