Load / Reload module

Discussion in 'Python' started by Marius Butuc, Feb 4, 2009.

  1. Marius Butuc

    Marius Butuc Guest

    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
    Marius Butuc, Feb 4, 2009
    #1
    1. Advertising

  2. Marius Butuc

    Peter Otten Guest

    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
    Peter Otten, Feb 4, 2009
    #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. Joshua Beall
    Replies:
    26
    Views:
    220,389
    AndreiKaa
    Jun 29, 2007
  2. gen_tricomi
    Replies:
    2
    Views:
    737
    gen_tricomi
    May 23, 2006
  3. Just D.
    Replies:
    5
    Views:
    444
    Just D.
    Jan 15, 2007
  4. Ron Rushing

    how to in a ASP page, load, pause and reload

    Ron Rushing, Sep 24, 2003, in forum: ASP General
    Replies:
    1
    Views:
    194
    Ray at
    Sep 24, 2003
  5. Stefan Mueller
    Replies:
    4
    Views:
    293
    Stefan Mueller
    Nov 5, 2005
Loading...

Share This Page