[IronPython] How do I run python functions and make the python code remember variables between calls from C#?

Tim Roberts timr at probo.com
Wed Mar 18 18:18:38 CET 2009


On Wed, 18 Mar 2009 00:47:30 -0700 (PDT), BlackMan890 <blackman at simnet.is>
> Hi there.
>
> When I run the following .py from C#:
>
> --- test.py ----------------
>
> global crap
> crap = None
>
> def set_crap():
> 	crap = "yay"
>
> def return_crap():
> 	return crap
> ---------------------------

If you are going to modify a global variable within a function, you must
use the "global" statement WITHIN THAT FUNCTION.  Viz:

    # test.py
    crap = None
    def set_crap():
        global crap
        crap = "yay"
    def return_crap():
        return crap

In your code, "crap" within "set_crap" is just a local variable, which
goes away when the function exists.

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.




More information about the Ironpython-users mailing list