You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/user/Python-on-JVM.md
+24-12Lines changed: 24 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -109,6 +109,7 @@ This enables you, for example, to use a Pandas frame as `double[][]` or NumPy ar
109
109
### Special Jython Module: `jarray`
110
110
111
111
GraalPy implements the `jarray` module (to create primitive Java arrays) for compatibility.
112
+
This module is always available, since we have not found its presence to have a negative impact.
112
113
For example:
113
114
114
115
```python
@@ -142,10 +143,7 @@ However, implicitly, this may produce a copy of the array data, which can be dec
142
143
143
144
### Exceptions from Java
144
145
145
-
To catch Java exceptions, use the `--python.EmulateJython` option.
146
-
147
-
> Note: Catching a Java exception incurs a performance penalty.
148
-
146
+
You can catch Java exceptions as you would expect.
149
147
For example:
150
148
151
149
```python
@@ -161,8 +159,8 @@ For example:
161
159
162
160
### Java Collections
163
161
164
-
* Java arrays and collections that implement the `java.util.Collection` interface can be accessed using the `[]` syntax.
165
-
An empty collection is considered `false` in boolean conversions.
162
+
* Java arrays and collections that implement the `java.util.Collection` interface can be accessed using the `[]` syntax.
163
+
An empty collection is considered `false` in boolean conversions.
166
164
The length of a collection is exposed by the `len` built-in function.
167
165
For example:
168
166
@@ -186,7 +184,7 @@ For example:
186
184
False
187
185
```
188
186
189
-
* Java iterables that implement the `java.lang.Iterable` interface can be iterated over using a `for` loop or the `iter` built-in function and are accepted by all built-ins that expect an iterable.
187
+
* Java iterables that implement the `java.lang.Iterable` interface can be iterated over using a `for` loop or the `iter` built-in function and are accepted by all built-ins that expect an iterable.
190
188
For example:
191
189
192
190
```python
@@ -242,12 +240,24 @@ For example:
242
240
243
241
### Inheritance from Java
244
242
245
-
Inheriting from a Java class (or implementing a Java interface) is supported with some syntactical differences from Jython.
246
-
To create a class that inherits from a Java class (or implements a Java interface), use the conventional Python `class` statement: declared methods
247
-
override (implement) superclass (interface) methods when their names match.
248
-
To call the a superclass method, use the special attribute `self.__super__`.
243
+
Inheriting from a Java class (or implementing a Java interface) is supported with some syntactical and significant behavioral differences from Jython.
244
+
To create a class that inherits from a Java class (or implements a Java interface), use the conventional Python `class` statement.
245
+
Declared methods override (implement) superclass (interface) methods when their names match.
246
+
247
+
It is important to understand that there is actually delegation happening here - when inheriting from Java, two classes are created, one in Java and one in Python.
248
+
These reference each other andany methods that are declared in Python that override or implement a Java method on the superclass are declared on the Java side as delegating to Python.
249
249
The created object does not behave like a Python object but instead in the same way as a foreign Java object.
250
-
Its Python-level members can be accessed using its `this` attribute. For example:
250
+
The reason for this is that when you create an instance of your new class, you get a reference to the *Java*object.
251
+
252
+
To call Python methods that do *not* override or implement methods that already existed on the superclass, you need to use the special `this` attribute.
253
+
Once you are in a Python method, your `self` refers to the Python object, and to get back from a Python method to Java, use the special attribute `__super__`.
254
+
And since we do not expose static members on the instance side, if you need to call a static method from an instance on the Java side, use `getClass().static` to get to the meta-object holding the static members.
255
+
256
+
One important consequence of the two-object-schema here is that the `__init__` method on the Python objectis actually called *before* the connection to the Java side is established.
257
+
So you cannot currently override construction of the Java objector run code during initialization that would affect the Java half of the combined structure.
258
+
You will have to create a factory method if you want to achieve this.
259
+
260
+
For example:
251
261
252
262
```python
253
263
import atexit
@@ -277,6 +287,8 @@ for record in handler.this.logged:
277
287
print(f'Python captured message "{record.getMessage()}" at level {record.getLevel().getName()}')
278
288
```
279
289
290
+
For more information about how the generated Java subclass behaves, see the [Truffle documentation](https://www.graalvm.org/truffle/javadoc/com/oracle/truffle/api/TruffleLanguage.Env.html#createHostAdapter(java.lang.Object%5B%5D)).
291
+
280
292
## Embedding Python into Java
281
293
282
294
The other way to use Jython was to embed it into a Java application. There were two options for such an embedding.
0 commit comments