[Tutor] Refreshing the interpreter environment

Kent Johnson kent37 at tds.net
Thu Jun 23 05:53:59 CEST 2005


Javier Ruere wrote:
> lawrence wang wrote:
> 
>>How do I refresh the interpreter environment without restarting it, if
>>possible? For example, I'm using the interpreter to test a class I'm
>>writing; importing and instantiating it reveals a typo; I go and fix
>>the typo. Now, is there any way to reload the class afresh? Simply
>>importing again doesn't seem to do it. Thanks in advance for your
>>help.
> 
> 
> Yes:
> 
> (echo "a=1" > a.py)
> 
>>>>import a
>>>>a.a
> 
> 1
> (echo "a=2" > a.py)
> 
>>>>a = reload(a)
>>>>a.a
> 
> 2

but be careful, if you try
from a import a
then the value of a will not change when a.py changes; you have created a new variable in your namespace that is bound to 1.

Also if there is a class defined in a.py and you create instances of the class, when you reload(a) the instances will still refer to the old class.

I'm sure there are many other ways this method can fail. You should have a clear understanding of namespaces to use it reliably; otherwise you may be surprised.

Kent



More information about the Tutor mailing list