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

Discussion in 'Python' started by Keith, Apr 12, 2011.

  1. Keith

    Keith Guest

    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
    Keith, Apr 12, 2011
    #1
    1. Advertising

  2. Keith

    Terry Reedy Guest

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

    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
    Terry Reedy, Apr 12, 2011
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Blum
    Replies:
    0
    Views:
    3,232
    Peter Blum
    Feb 13, 2004
  2. Joshua Beall
    Replies:
    26
    Views:
    220,404
    AndreiKaa
    Jun 29, 2007
  3. gen_tricomi
    Replies:
    2
    Views:
    737
    gen_tricomi
    May 23, 2006
  4. Brian

    Fly outmenu on the fly

    Brian, Apr 8, 2005, in forum: Javascript
    Replies:
    0
    Views:
    103
    Brian
    Apr 8, 2005
  5. Stefan Mueller
    Replies:
    4
    Views:
    294
    Stefan Mueller
    Nov 5, 2005
Loading...

Share This Page