Load / Reload module

M

Marius Butuc

Hi,

First I must state that I'm a beginner in Python, so all help would be
more than welcomed.

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
- from classes import * # imports, but I can't reload
- reload(classes) # I have the same class definition
after this

I've tried the documentation but I got lost, so please help.

Thanks,
Marius
 
P

Peter Otten

Scott said:
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:
.... def __str__(self): return 'new'
.... """)old

Sometimes you may be able to fix this manually by assigning to the
__class__:
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.pynew

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top