-1

I was told in another language that it is bad practice to call the equivalent to quit() or exit() in Python.

Rather then calling a similar function to quit() or exit() in Python that it is better to let the program end "naturally". In that the main document ends after completing any function/method calls or any condition checking, etc...

Is this also true in Python or is it fine to use these functions for ending the program?

Edit: I will include a link to my actual code to hopefully add some clarification, it is the bottom two function that I'm having issues with

https://github.com/itwasntzak/delivery_tracking-python/blob/test/menus.py

  • you need to describe your particular case because answer depends on it – Azat Ibrakov Jun 24 '20 at 03:53
  • In other languages, it is common that those are equivalent to an abort, historically. It could leave ports open, files not fully written, directories not updated, device states in an unknown state, allocated memory unreleased, all sorts of ugliness. Over the years many of those issues have been dealt with and the underlying os cleans up your mess. But an orderly, controlled exit is always preferable. – RufusVS Jun 24 '20 at 03:54
  • @AzatIbrakov I'm using while loops as user selection like a menu. I have one function that is supposed to be a menu, depending what character the user enters it does something specific. The main menu function calls another menu function that does the same looping as a user selection. In order to complete and leave the second menu you must enter one character. I want to be able to be able to enter a different key to exit the program without completing the second menu. If I leave the second loop without exit, then it returns to the first menu, doesn't completely exit the program like I want. – ItWasntZak Jun 24 '20 at 04:15
  • Please do not use `quit()` or `exit()` in your code. These functions are intended only for the interactive Python. See [Python exit commands - why so many and when should each be used?](https://stackoverflow.com/a/19747562/320437) --- Normally you will implement your program in a `main()` function (best practice) you can let execute its commands till the end or end it prematurely by executing the `return` statement. – pabouk - Ukraine stay strong May 11 '22 at 17:17

1 Answers1

1

I have never come across where there is a need for a hard stop like that. You are right that it is common practice to let the program execute in full. If you need to break out of a loop then you can use break. Is there a specific example where you think you need to use a quit() tag?

Matt Carroll
  • 90
  • 1
  • 7
  • I have menu like functions, where the user enters one character to select a menu option. While loops is how I'm achieving recursion without actually doing recursion in the condition of my menus. One menu option of my main menu is the create a specific object that has other variable number of objects in it. I use a second menu to allow the user to add the sub objects, I want one of the menu options to be to exit without returning to the first menu. If I complete entering data on the main object I want it to return that to the main "menu" loop. Second loop goes to main loop if I break normally – ItWasntZak Jun 24 '20 at 04:21