On Mar 13, 7:02 am, Bruno Desthuilliers <bruno. [EMAIL PROTECTED]> wrote: > Alex a écrit : > (sni) > > > First of all thanks all for answering! > > > I have some environment check and setup in the beginning of the code. > > I would like to move it to the end of the script. > > Why ? (if I may ask...) > > > But I want it to > > execute first, so the script will exit if the environment is not > > configured properly. > > If you want some code to execute first when the script/module is loaded, > then keep this code where it belongs : at the beginning of the script.
I concur with Bruno's recommendation: stuff you want to do first should come first in the script. Things like BEGIN blocks hurt readability because you can't identify where execution begins without reading the whole file. Having said that, one thing that often happens in Python scripts is that all the functions are defined first, then the script logic follows. So you could put the meat of your script in a function, then the "BEGIN" stuff after that functions: def run_script(): # # script contained in this long function # # Then test preconditions here... if os.environ["HELLO"] != "WORLD": sys.exit(2) # Then call the run_script functions run_script() But having said THAT, I don't recommend you do that with preconditions. If the script has a quick early exit scenario, you really ought to put that near the top, before the function definitions, to clearly show to a human reader what is necessary to run the script. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list