Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Testing changes to Python code on the fly: reload() + alternatives?

Reply
Thread Tools

Testing changes to Python code on the fly: reload() + alternatives?

 
 
Keith
Guest
Posts: n/a
 
      04-12-2011
Hi all,

I have a couple of questions that I was hoping people might be able to provide suggestions on.

1) Is it possible to reload a class using the reload() method? For instance, suppose you have the following files:

my_module/
__init__.py
MyClass.py

#init.py
from MyClass import MyClass

#MyClass.py
class MyClass:
def __init__(self):
print("Before...")

Is it possible to load the class, change the print statement (e.g. print("after...")), and have the changes take effect right away?

The above method does not work:

In [1]: import my_module

In [2]: my_module.MyClass()
Before...
Out[2]: <my_module.MyClass.MyClass instance at 0x8f5d16c>

In [3]: reload(my_module)
Out[3]: <module 'my_module' from 'my_module/__init__.pyc'>

In [4]: my_module.MyClass()
Before...
Out[4]: <my_module.MyClass.MyClass instance at 0x8fca1ec>

Is there anyway to make this work while keeping MyClass in the main "my_module" namespace?

2) More generally though, what is the best way to go about testing changes to code as you are writing it?

It would be very convenient to be able to use reload() along the lines of the above so that I could test changes to a small part of some class/function/etc without having to reload the entire state from scratch.

Another option of course would be to just run everything as a standalone script, but then you lose the ability to be able to easily inspect the objects you are working and change them on the fly, except perhaps by using PDB.

Any suggestions would be greatly appreciated.

Thanks!
Keith
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      04-12-2011
On 4/12/2011 3:26 PM, Keith wrote:

> 1) Is it possible to reload a class using the reload() method?


Yes, but instances of the old version will remain instances of the old
version, which will not go away until all its instances and other
references go away. So reload is deceptive, which is why it was
semi-hidden away in 3.x in the inspect module.

>>> class C: pass
>>> c = C()
>>> class C: pass # simulate reload
>>> c2 = C()
>>> id(c.__class__)

16018776
>>> id(c2.__class__)

16039320

> 2) More generally though, what is the best way to go about testing changes to code


Restart from the beginning.

--
Terry Jan Reedy

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Webinar - Testing the Untestable - Unit Testing Dependencies andLegacy Code (in C++) Avi C Programming 0 06-11-2012 01:55 PM
[ANN] Webinar - Testing the Untestable - Unit Testing Dependenciesand Legacy Code in C++ Avi C++ 0 06-06-2012 11:25 AM
Is there anyway Vpython and pyODE can be made to work with newer versions of Python 2.6.1 etc. without a lot of changes to source code? Casey Hawthorne Python 1 01-24-2009 09:27 PM
Re: testing -- what to do for testing code with behaviour dependantupon which files exist? Brian van den Broek Python 2 04-04-2005 11:37 PM
testing -- what to do for testing code with behaviour dependant uponwhich files exist? Brian van den Broek Python 3 04-03-2005 12:28 AM



Advertisments