load changes of subclasses

T

Tom

Hi,

I have a main program that uses a lot of definitions in different classes.
They are included like this:
from subclass1 import *
from subclass2 import *
My problem is: whenever I make changes in one of the classes I have to
close and reopen the main program for the changes to take effect. This
is kind of annoying, because I always want to test if my changes are
correct and that's why I have to run the main program. Is there some
kind of a shortcut where I don't have to close and reopen the main program?

I hope this question is not too stupid, but I am still quite new to Python.
Thanks a lot.
Regards, Tom
 
G

Gerrit Holl

Tom said:
Date: Tue, 23 Sep 2003 16:56:55 +0200

Hi,

I have a main program that uses a lot of definitions in different classes.
They are included like this:
from subclass1 import *
from subclass2 import *

Note that 'subclass1' and 'subclass2' are not classes, but modules. A
class is created with the 'class' statement:

class MyClass:
def method(self):
return "Hi there!"

A module is a file in the library.
My problem is: whenever I make changes in one of the classes I have to
close and reopen the main program for the changes to take effect. This
is kind of annoying, because I always want to test if my changes are
correct and that's why I have to run the main program. Is there some
kind of a shortcut where I don't have to close and reopen the main program?

You can use the reload() function for that: reload(module) -> module

Reload the module. The module must have been successfully imported before.
I hope this question is not too stupid, but I am still quite new to Python.

Stupid questions do not exist (hm, my first post here makes me think otherwise),
stupid answers do, so:

I hope this answer is not too stupid, but I am not a Computer Scientist.

yours,
Gerrit.
 
B

Bob Gailer

Note that 'subclass1' and 'subclass2' are not classes, but modules. A
class is created with the 'class' statement:

class MyClass:
def method(self):
return "Hi there!"

A module is a file in the library.


You can use the reload() function for that:
reload(module) -> module

Reload the module. The module must have been successfully imported before.

BUT realize that reloading does not rebind the names bound by the original
from subclass1 import *. This is one reason to either avoid from import, or
else:

try:del sys.modules['subclass1']
except:pass
from subclass1 import *

Bob Gailer
(e-mail address removed)
303 442 2625
 
J

Jp Calderone

Hi,

I have a main program that uses a lot of definitions in different classes.
They are included like this:
from subclass1 import *
from subclass2 import *
My problem is: whenever I make changes in one of the classes I have to
close and reopen the main program for the changes to take effect. This
is kind of annoying, because I always want to test if my changes are
correct and that's why I have to run the main program. Is there some
kind of a shortcut where I don't have to close and reopen the main program?

Others have suggested how you might get changes on disk reflected in your
running app, so I'll skip that.

Wanting to test if your changes are correct is a very good thing, and
doing that by running a program that uses those changes is also a pretty
handy idea. But maybe running your *main* program isn't the best way to go
about it. Many developers use an approach called "unit testing" to verify
that each component of their application works as it should. This makes
testing easier because it removes the interactive component, because it
provides a fine grained indicator of which part of your application is
broken, and because unit tests can be run quickly and frequently, which is
especially handy when you're only changing a small part of a large
application.

You can read more about unit testing on the Python website:

http://python.org/doc/lib/module-unittest.html

Jp
 

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

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top