From: <bc...@wo...> - 2001-10-23 19:10:32
|
[Andres Corrada-Emmanuel] >Apparently the answer is no if you plan to turn that python class back >into a java class. Right. >Is this a bug or am I not getting how to turn python >classes into java classes? > >I have the following python class that subclasses from java.util.Random. >It's a modification of the rand.py code shown in the jython website: > >rand.py: > >from java.util import Random > >class rand(Random): > def __init__( self ): > "@sig public rand()" > Random.__init__( self ) > > def nextDouble( self ): > return Random.nextDouble( self ) > >This is turned into a java class with jythonc. It is tested with this >java file: > >TestRand.java: > >import rand; > >class TestRand >{ > public static void main(String args[]){ > rand firstRand = new rand(); > System.out.println( firstRand.nextDouble() ); > } >} > >Executing this java file leads to an "instance already instantiated" >error. If this sounds familiar to you maybe you've read my previous >two emails to the list on this problem. Nobody seems to care :-( Thats wrong; I care, but I am too busy to spend much time on it at the moment. >The code works if you take Random.__init__(self) out of rand.py. But >this means you cannot call the super class constructor within your own >init. This sucks! Right, and there is *no* way we can fix that. Remember that rand is a true subclass of Random and there is no way we can execute any jython (or java) code in the constructor before calling super(). Looking at the ctor in rand.java the problem is obvious: public rand() { super(); __initProxy__(new Object[] {}); } The call to __initProxy__ will eventually call rand.__init__ but then the Random ctor have been called already. >It must be something I'm doing wrong because the website >explicitly mentions that you CAN call the super class constructor. Yes, but that is when the python instance is created before the proxyinstance. When creating the class from java, the proxyinstance is create first and then we have to accept the arguments pass from the java side. >Please, please, please help me out of my quagmire. I'm dying to use Python >and if this doesn't work for me I'll be condemned to writing Java code. Try to change rand.py to (without any @sig for the ctor): from java.util import Random class rand(Random): def __init__( self): pass def nextDouble( self ): return Random.nextDouble( self ) That will create the two java ctors: public rand(long arg0) { super(arg0); __initProxy__(new Object[] {Py.newInteger(arg0)}); } public rand() { super(); __initProxy__(new Object[] {}); } and you can then create an instance of "new rand(42)" from the java test program. You still can't call the Random.__init__ from python. I hope this helps. regards, finn |