11 Aug 13:18
Fwd: Using __tojava__ - where is the class code running ?
From: Tobias Ivarsson <thobes <at> gmail.com>
Subject: Fwd: Using __tojava__ - where is the class code running ?
Newsgroups: gmane.comp.lang.jython.user
Date: 2008-08-11 11:18:27 GMT
Subject: Fwd: Using __tojava__ - where is the class code running ?
Newsgroups: gmane.comp.lang.jython.user
Date: 2008-08-11 11:18:27 GMT
Yeah, that's right, forgot that this list doesn't have reply-to headers set to the list...
---------- Forwarded message ----------
From: Tobias Ivarsson <thobes <at> gmail.com>
Date: Mon, Aug 11, 2008 at 1:17 PM
Subject: Re: [Jython-users] Using __tojava__ - where is the class code running ?
To: David Griffin <gripfin <at> gmail.com>
All code executed by Jython is always compiled to Java bytecode.
When Jython first encounters your code (for example when you import your module) Jython will compile all the code to Java bytecode. That means that all the code bodies will be compiled to Java bytecode. A class definition in Python is just a code body that defines a bunch of bindings in a locals dictionary followed by a call to some makeClass system level method. In Jython when the class definition is executed this makeClass method will look at the bases you have defined, identify the Java interfaces you have defined there and create a Java class with the apropriate methods. These methods will, when invoked, lookup the apropriate name from the Jython object and invoke that functio n. The __tojava__ method will simply return an instance of this Java class.
It was a good question, thank you for asking! I hope my answer clarified things somewhat for you.
I am not sure on this one... does someone else have an idea?
/Tobias
From: Tobias Ivarsson <thobes <at> gmail.com>
Date: Mon, Aug 11, 2008 at 1:17 PM
Subject: Re: [Jython-users] Using __tojava__ - where is the class code running ?
To: David Griffin <gripfin <at> gmail.com>
On Mon, Aug 11, 2008 at 8:40 AM, David Griffin <gripfin <at> gmail.com> wrote:
I have a java abstract class "uiscript". It has an abstract method "display"
At runtime I create an interpreter and read some Jython text from
somewhere (myJythonClass.py)
This text contains a class definition for a class that overrides
"uiscript" and which implements the display() method.
I execute this Jython text then I get a class object, and execute that
to instantiate an object of this class. Using __tojava__ I get an
object representing an instance of the class.
In Java I can now treat this object as an instance of uiscript and
execute the display() method.
Here's the question.
What is this Java class (of which this object is an instance) exactly
? Is it a java wrapper round a class whose method code is in Jython ?
Or have I now got an instance of a fully compiled java class,
compiled on the fly as part of the __tojava__ process ? Put another
way, is the Jython code in the display() method (and any other Jython
methods it calls) going to be executed by the interpreter or as Java
byte code ?
All code executed by Jython is always compiled to Java bytecode.
When Jython first encounters your code (for example when you import your module) Jython will compile all the code to Java bytecode. That means that all the code bodies will be compiled to Java bytecode. A class definition in Python is just a code body that defines a bunch of bindings in a locals dictionary followed by a call to some makeClass system level method. In Jython when the class definition is executed this makeClass method will look at the bases you have defined, identify the Java interfaces you have defined there and create a Java class with the apropriate methods. These methods will, when invoked, lookup the apropriate name from the Jython object and invoke that functio n. The __tojava__ method will simply return an instance of this Java class.
Could the interpreter go out of scope while I still have a valid
reference to this new object in Java ?
I suspect that when I fully understand what is happening here I will
realise the question is the wrong question but I only learn by
asking...
It was a good question, thank you for asking! I hope my answer clarified things somewhat for you.
One other question. If I create many many class definitions in my
interpreter in a given session
- are they just in memory ? Or is there any disk usage related to this
If Jython has a writable cache area Jython will store the java bytecode for the code bodies that you have defined, kinda like CPython stores pyc-files.
- how do I remove a definition of a class I don't intend to use any
more to free up some resources ?
I am not sure on this one... does someone else have an idea?
/Tobias
------------------------------------------------------------------------- This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________ Jython-users mailing list Jython-users <at> lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jython-users
RSS Feed