[IronPython] Clearing context

Lee Culver leculver at microsoft.com
Wed Jan 9 18:42:51 CET 2008


I don't know the answer to the original question, but this is not exactly correct:

Another alternative would be to change your code to look like this:

class MyClass(SomeNameSpace.IMyInterface):
    def SomeFunc(self):
        from mymod import myfunc
        myfunc()

That way, the import gets re-run each time you run SomeFunc, and
you'll always get the latest version of the module.

This will not re-import mymod every time the function is run.  Once you run "from foo import bar" or "import foo" then the "foo" module is loaded into python and will not be reloaded from python without an explicit call to the reload function.  For example, let's say you have the following code in mymod.py:

def foo():
    print "called foo()"

def bar():
    print "called bar()"

print "Loaded..."

And then you run this code:
def test1():
    from mymod import foo
    foo()
def test2():
    from mymod import bar
    bar()

test1()
test2()

import mymod
mymod.foo()

"Loaded..." will be printed only once, because the module is only loaded once, and never reloaded.  This is a very useful feature which Python has supported for quite some time, allowing you to sprinkle "from foo import blah" around your code, and if and only if you hit one of these will the module be loaded, and it will only be loaded once.

Back to the original question, are you saying that if you restart your C# program that the changed "mymod" is not loaded properly, or are you modifying the script while the C# program is running?

-Lee





-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Curt Hagenlocher
Sent: Wednesday, January 09, 2008 9:16 AM
To: Discussion of IronPython
Subject: Re: [IronPython] Clearing context



On Jan 9, 2008 8:54 AM, Slide <slide.o.mix at gmail.com> wrote:

>

> If I change this file and try reloading my script, it takes the

> modifications just fine and runs the new code. If I modify mymod, but

> not the main script, I get the old code from mymod run. Is there

> something I can do to force it to reload the imported scripts as well?



There's no straightforward and reliable way to track usage, so the

only thing you could do generically is to reload *all* your modules.

One thing you could do would be to say "import mymod" in any other

module using mymod.  After a reload of mymod, you could then iterate

over sys.modules and reload any module containing the symbol "mymod".

It's not foolproof, but it's probably good enough.



Another alternative would be to change your code to look like this:



class MyClass(SomeNameSpace.IMyInterface):

    def SomeFunc(self):

        from mymod import myfunc

        myfunc()



That way, the import gets re-run each time you run SomeFunc, and

you'll always get the latest version of the module.



--

Curt Hagenlocher

curt at hagenlocher.org

_______________________________________________

Users mailing list

Users at lists.ironpython.com

http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20080109/dbf6abf6/attachment.html>


More information about the Ironpython-users mailing list