Quitting a Python script early

I am working on a popup which contains a number of data entry controls. When the user presses the Save button, I want to carry out a number of checks to ensure the data is valid. If any one of these checks fail, I would like to quit the script immediately without carrying out the remaining checks. Is this possible?

The best I’ve managed to come up with is setting a flag on failure, and checking this flag is not set before each subsequent check.

No, there isn’t. There are two ways to do what you want, one being the flag idea that you already discovered. The other is to put your whole routine in a function so that you can use the return keyword, and then call than function, like so:

def doIt(event): do stuff... if bad input: return do other stuff doIt(event)


Defining a function and using return works well. The only thing I found was that I had to import fpmi inside the function in order to use the runQuery function. Why is this and will it have any adverse affects (such as on memory usage)?


We automatically import fpmi and app for you before an event script runs, but when you define a new function, that function has its own namespace that isn’t related to the defining scope, so you need to import fpmi again.

This will not have any adverse effects - imports are cheap.

That’s what is ruining the economy :laughing:

Heh, yeah, don’t we know it here in the Land of Walmart.