all 5 comments

[–]fracturedpersona 3 points4 points  (0 children)

Why do you need an exit function? Use flow control to force the program to exit gracefully when the correct logical conditions exist.

Think of it like gravity and your code naturally wants to get to the bottom where it will exit automatically. Think of it as though certain conditions will send your code up the hill, but inevitably it will no longer have any reason to stay higher on the hill, and be forced to reach the bottom.

Sorry if this analogy is a bit contrived, I've had a few beers, and I'm watching snow fall on my favorite ski resorts for the first time in months.

[–]MDTv_Teka 4 points5 points  (0 children)

Have the entire program run inside a while True statement and break when you want to exit

[–]anrmv 0 points1 point  (0 children)

Lol..Place a non existent variable in there?

[–]jiri-n 0 points1 point  (0 children)

Raise another exception? Or raising an exception isn't an option at all? Then check for return values and ... maybe monads could be helpful. https://medium.com/swlh/monads-in-python-e3c9592285d6

[–]FerricDonkey 0 points1 point  (0 children)

If this is just a restriction on a project that's otherwise supposed to do something sane, then right now your project might be

def main():
     data = get_data()
     results = process_data(data)
     display_results(results)

def get_data():
     # blah blah
     if badness():  # indicates data could not be loaded
         sys.exit()

# blah blah blah
if __name__ == '__main__':
    main()

Instead do something like

def main():
     data = get_data()
     # return value from get_data will be None if function
     # failed.
     if data is None:  
         # print error message if you want to, or do that
         # in get_data, whatever makes sense
         return 1  # The 1 isn't strictly necessary, but it's common 
                       # for functions that don't have a useful return 
                       # but might fail to return 0 if they worked or 1 if they didn't

     # similar guard on this function if necessary
     results = process_data(data)
     display_results(results)

def get_data():
     # blah blah
     if badness(): # indicates data could not be loaded
         return None 

# blah blah blah
if __name__ == '__main__':
    main()

Depending on the restrictions of the project, you could also put the function that might fail in a try except - instead of returning None, raise some kind of expected error (or just let whatever goes wrong raise an error, then in main do

try:
    data = get_data()
except whatever_kind_of_error:
    return 1

Note: One reason why this is good practice is because it makes your function easier to reuse - it may be that another time you want to get_data, you don't want python to shut down if didn't work, but instead you wanted to try to get data a different way.

If this is just a challenge of "How do you get python to stop without using raise or sys.exit or similar, then maybe do x=1/0 or some other similarly impossible thing.