Scott David Daniels wrote:

> Marius Butuc wrote:
>> I want do declare some classes (classes.py) in an external editor,
>> than import the file and use the classes. After I change the file, I
>> want to reload the definitions w/o leaving the interactive
>> interpreter.
>> 
>> So far I have tried
>> - import classes               # doesn't import my classes
> Use this and refer to the class from the imported module.
> 
>      import classes
>      instance = classes.SomeClass()
>      ...
>      reload(classes)
>      instance = classes.SomeClass()
> 
> This is by far the best way, but if you _must_,
>      from classes import *
>      instance = SomeClass()
>      ...
>      reload(classes)
>      from classes import *
>      instance = SomeClass()

Also note that only instances created after the reload will have the new
layout:

>>> import classes
>>> a = classes.SomeClass()
>>> print a
old
>>> open("classes.py", "w").write("""class SomeClass:
...   def __str__(self): return 'new'
... """)
>>> reload(classes)
<module 'classes' from 'classes.py'>
>>> b = classes.SomeClass()
>>> print b
new
>>> print a
old

Sometimes you may be able to fix this manually by assigning to the
__class__:

>>> a.__class__ = classes.SomeClass
>>> print a
new

but I recommend that you put all your code into a another script rather than
resorting to such tricks

$ cat main.py
import classes
a = classes.SomeClass()

If you want to experiment with a in the interactive interpreter you can
invoke main with the -i option:

$ python -i main.py
>>> print a
new

Peter
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to