From: Paul D. F. <pdf...@ku...> - 2005-11-26 19:27:44
|
More on this: Drat, it doesn't work for instance methods, producing a "readonly class or attribute" error in Jython 2.1. Wonder why? I don't see anything obvious in the Java code (lookign at 2.2 though), and I'm assuming this is using a PyFunction too? Funny how I can still assign a method to a regular function though! So, I try it in 2.2 from CVS. And I get: >>> Z.foo.func_code = Q.foo.func_code Traceback (innermost last): File "<console>", line 1, in ? AttributeError: 'method' object has no attribute 'func_code' Transcripts for each version: ============== $ java -jar jython.jar jython: pdf modified version from CVS Jython 2.2a1 on java1.4.2-02 (JIT: null) Type "copyright", "credits" or "license" for more information. >>> class Z: ... def __init__(self): self.test = 10 ... def foo(self): print "foo1", self.test ... >>> Z().foo() foo1 10 >>> class Q: ... def __init__(self): self.test = 11 ... def foo(self): print "foo2", self.test ... >>> Q().foo() foo2 11 >>> Z.foo.func_code = Q.foo.func_code Traceback (innermost last): File "<console>", line 1, in ? AttributeError: 'method' object has no attribute 'func_code' >>> Z <class __main__.Z 1> >>> Z.foo <unbound method Z.foo> >>> Z.foo.func_code <code object foo 2, file "<console>", line 3> >>> Q.foo <unbound method Q.foo> >>> Q.foo.func_code <code object foo 3, file "<console>", line 3> >>> Z.foo.func_code = Q.foo.func_code Traceback (innermost last): File "<console>", line 1, in ? AttributeError: 'method' object has no attribute 'func_code' ======================== $ jython Jython 2.1 on java1.4.2-02 (JIT: null) Type "copyright", "credits" or "license" for more information. >>> class Z: ... def __init__(self): ... self.test = 10 ... def foo(self): print "foo1", self.test ... >>> Z().foo() foo1 10 >>> class Q: ... def __init__(self): ... self.test = 11 ... def foo(self): print "foo2", self.test ... >>> Q().foo() foo2 11 >>> Z.foo.func_code <code object foo at 32567496, file "<console>", line 4> >>> Z.foo.func_code = Q.foo.func_code Traceback (innermost last): File "<console>", line 1, in ? TypeError: readonly class or attribute: func_code >>> Q.foo.func_code <code object foo at 30015102, file "<console>", line 4> >>> Z.foo.func_code = Q.foo.func_code Traceback (innermost last): File "<console>", line 1, in ? TypeError: readonly class or attribute: func_code >>> def x(): pass ... >>> x.func_code <code object x at 22081762, file "<console>", line 1> >>> x.func_code = Q.foo.func_code >>> x() Traceback (innermost last): File "<console>", line 1, in ? TypeError: foo() takes at least 1 argument (0 given) >>> x(None) foo2Traceback (innermost last): File "<console>", line 1, in ? File "<console>", line 4, in foo AttributeError: 'None' object has no attribute 'test' >>> x(Q()) foo2 11 >>> x(Z()) foo2 10 >>> Q.foo.func_code <code object foo at 30015102, file "<console>", line 4> >>> Q.foo.func_code = Z.foo.func_code Traceback (innermost last): File "<console>", line 1, in ? TypeError: readonly class or attribute: func_code >>> Q().foo.func_code = Z.foo.func_code Traceback (innermost last): File "<console>", line 1, in ? TypeError: readonly class or attribute: func_code >>> ============== So, apparently this functionality has changed from Jython2.1, although it doesn't work as I hoped for in either version. More exploring to do. --Paul Fernhout |