Jython inherit from Java class
Kent Johnson
kent at kentsjohnson.com
Sat Feb 11 08:28:01 EST 2006
Mark Fink wrote:
> Please enlighten me. This seemed so easy yust inherit from a Java class
> overwrite one method - done. At the moment I do not know how to proceed
> :-((
It should be easy.
> Jython or jythonc?
> ==================
> In general is it better to run programms with the jython interpreter or
> is it better to compile them first? I had the impression that the
> inheritance does not work with the interpreter. Is that right?
I personally avoid jythonc, I have not had success with it. Others
apparently have and use it.
Inheritance works fine without jythonc. The limitation in non-compiled
Jython is in the methods that are visible to Java code. Java code will
only see methods of a Jython class that are declared in a Java class or
interface that the Jython class extends or implements.
For example:
Java:
public class Foo {
public int value() { return 3; }
}
public interface Bar {
public int anotherValue();
}
Jython:
class Baz(Foo, Bar):
def value(self): return 5
def anotherValue(self): return 7
def somethingCompletelyDifferent(self): return 42
Now, a Java client that has an instance of Baz will be able to call
baz.value()
baz.anotherValue()
but
baz.somethingCompletelyDifferent()
will not be possible even using Java introspection.
OTOH a Jython client will be able to access all three methods of baz.
>
> 2.1 or 2.2a
> ===========
> The programm did not compile with the 2.1 version and Java 1.5. But
> with 2.2a it compiled but 2.2a is an alpha version. Assuming the
> programm would compile with the Java 1.4 version would it be better
> (saver, more stable, performance) to use 2.1 or 2.2a?
IMO Jython 2.2a1 is not near ready for production use. Jython 2.1 is
very stable and usable.
> Inherit from java baseclass or reimplement the whole thing in jython?
> =====================================================================
> All the problems with Jython stated when I wanted to inherit from a
> Java baseclass. I wounder if I would have more success when
> implementing it in pure Jython. Regarding the inheritance I had an
> empty wrapper arround the Java baseclass and 70% of the unit test
> passed. But the performance was ~ factor 10 slower than using the base
> class directly. Does this mean that there is overhead with the
> inheritanc construct and that this implementation path would always be
> so slow. On the other hand would a 100% Jython solution be faster?
Inheritance from Java works well and I have never seen performance
problems like this.
Java JUnit uses introspection to find test methods so it won't find
methods of a Jython class.
HTH,
Kent
More information about the Python-list
mailing list