[Jython] Pass variable or class reference to PythonInterpreter?

Alex Martelli aleax at aleax.it
Thu Nov 7 03:56:54 EST 2002


Robert Oschler wrote:

> I know I can compile a Jython class and then have it accessible as a true
> Java class to other java code. But I'm wondering if I can pass a variable
> or
> class reference to an instance of PythonInterpreter?  For example:
> 
>     private void CallInterpreter()
>     {
>         String theString = "Hello";
> 
>         PythonInterpreter interp = new PythonInterpreter();
> 
>         // Here I enter a loop receiving python commands and passing them
>         to
> "interp.exec()"
>         // So how could I give "interp" access to "theString" so commands
> passed to "exec()"
>         //  can see it, print it, change it, etc?
>     }
> 
> Is there a way to do this?
> 
> thx

Sure, Jython's PythonInterpreter class gives you methods you
can use for this purpose.  interp.set("theString", theString)
for example would add an attribute named "theString" to the
inteprreter's local namespace, with its value set to the Java
value theString.  *CHANGING* it is obviously out of the question
(strings are immutable in Java *AND* in Python), but THAT
is clearly a completely different issue.


Alex




More information about the Python-list mailing list