On 12/30/06, Luke Paireepinart <[EMAIL PROTECTED]> wrote: > > > I got confused now:-> if IDLE keeps the imports in between runs of the > > program, do I still need import and reload? or click F5 in IDLE is OK? > > > The purpose of import is so that python knows what packages you intend > to use, > because if it imported every package every time, then many bad things > would occur > - one broken package would cause every python script on your computer to > stop running until you deleted or fixed the problem package > - it would clutter the global namespace > - it would slow down startup of your scripts. > - various other things that I can't think of. > > So this explains why we need import. > > Now in IDLE, because it is itself written in Python, > there are strange side effects (when it can't open a subprocess). > If you write a script in IDLE that goes into an infinite loop, the IDLE > windows will stop responding. > As we mentioned before, if you run one script through an IDLE shell that > imports a module, > on all the subsequent runs, even of different scripts, through the same > IDLE shell, the imports will still be in the global namespace. > > That does not mean that you don't need to have an import at the top of > each of your scripts. > You want the program to run outside of IDLE, right? How about in a > fresh copy of IDLE? > then you better put the imports in. > Python won't know what you mean when you say something like 'print > math.pi' unless you have an 'import math' at the beginning of your script. > > As far as reload goes: > > reload does NOTHING if you're running your script from the command line > (as far as you're concerned, at this stage in learning Python.). > Take this for example: > > #----- config.py > a = 'apples' > #----- end > > #----- script.py > > import config > print a > reload(config) > print a > > #------ > The output of this is going to be > apples > apples > unless you change the 'config.py' file in between the first 'print a' > and the 'reload'. > > In other words, reload updates changes that you make to your > modules/packages. > If you aren't planning on editing modules while your script is running, > you don't have to worry about what 'reload' does. > The imports will always be reloaded automatically when you run the > Python interpreter again. > > There are 2 situations, as a beginner, that you would want reload. > Take this for example. > > #-----config.py > a = 'apples' > #----- > > >>> import config > >>> print config.a > apples > > #------- we go and edit config.py while the interactive interpreter is > still running, and we change it to this: > a = 'aardvark' > #----- > > >>> print config.a > apples > >>> reload(config) > >>> print config.a > aardvark > > See, the 'config' module's attributes didn't change until we reloaded > it. So case 1: when using an interactive interpreter and editing modules, > reload updates our changes. > > The second case is, if IDLE is running, remember how we said that it > keeps the imported modules imported? > > Well, say your program does this: > #---- > import config > print config.a > #----- > Now, the first time, and all subsequent times we run this program, it > will do the same thing: > it will print whatever 'a' was when config was imported in the FIRST run > of the program. > So if we went and edited config.py, none of the changes would be > reflected in the print statement of this script. > > Basically, if you're designing a module in IDLE and you're making lots > of changes to it, you have 2 choices: > First, you could add a reload() to the top of the script that's using > the module, > and take it out when you're done. > > Or, a better solution, start IDLE from the start menu, instead of > right-clicking and choosing "Edit..." and it will open a subprocess, > and none of these 'imported stuff hanging around' problems will occur. > > HTH - tell me if anything's unclear > -Luke > > Thanks a lot! _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor