a little help

Lie Ryan lie.1296 at gmail.com
Thu Jan 5 03:30:37 EST 2012

On 01/05/2012 11:29 AM, Andres Soto wrote:
> my mistake is because I have no problem to do that using Prolog which
> use an interpreter as Python. I thought that the variables in the main
> global memory space (associated with the command line environment) were
> kept, although the code that use it could change.
> As you explain me, Python behave like a compiled language: any time I
> make a change in the code, I have to "compile" it again, and re-run (and
> re-load the data). There is nothing to do.

it is usually trivial to redefine state-free functions, you just need to 
copy and paste the new code into the shell. Redefining a class is a bit 
more complicated, while you can redefine a class by the same technique 
(copy pasting the new class definition to the shell), it will not modify 
the class definition for existing instances of that class. Worst comes 
to worst, you could end up with a list of instances where half of the 
items come from the old definition and the other half from the new 

If your global data are only of native types (e.g. list, dict, int, 
float), then you usually can safely carry your data between 
redefinitions; if you have objects in your global data that you want to 
preserve, you need to be really careful not to confuse instances from 
old definitions with instances from new definitions.

Also, reload() will reload a module with the new definition, but it does 
not touch existing function definitions in the global namespace; 
therefore if you want to use reload(), you probably should avoid "from 
... import ..." (if you want to import module functions into your global 
namespace, then you'll need to reimport them after you reload the module).

So here's the gotchas to be aware of when reloading modules:

1. import is cached, if you want to reimport a changed module you have 
to call reload()
2. reload does not modify anything in existing global namespace, if you 
have imported functions/class definition to your global namespace, you 
will need to reimport them after reloading.
3. be careful if you mix instances made from old definitions with 
instances made from new definitions, python does not modify the class 
definition of existing instances
4. be careful when reloading functions that have function attributes. 
The same caution applies when reloading class that have class attributes.

More information about the Python-list mailing list