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
See [Polyglot Programming](https://www.graalvm.org/docs/reference-manual/polyglot-programming/) and [Embed Languages](https://www.graalvm.org/reference-manual/embed-languages/) for more information about interoperability with other programming languages.
186
+
187
+
## The Behaviour of Types
188
+
189
+
The interop protocol defines different "types" which can overlap in all kinds of
190
+
ways and have restrictions on how they can interact with Python.
191
+
192
+
### Interop Types to Python
193
+
194
+
Most importantly and upfront - all foreign objects passing into Python have the
195
+
Python type `foreign`. There is no emulation of i.e., objects that are interop
196
+
booleans to have the Python type `bool`. This is because interop types can
197
+
overlap in ways that the Python builtin types cannot, and it would not be clear
198
+
what should take precendence. Instead, the `foreign` type defines all of the
199
+
Python special methods for type conversion that are used throughout the
200
+
interpreter (methods like `__add__`, `__int__`, `__str__`, `__getitem__` etc)
201
+
and these try to do the right thing based on the interop type (or raise an
202
+
exception.)
203
+
204
+
Types not listed in the below table have no special interpretation in Python
| Null | It is like None. Important to know: interop null values are equal, but not identical! This was done because JavaScript defines two "null-like" values; `undefined` and `null`, which are *not* identical |
210
+
| Boolean | Behaves like Python booleans, including the fact that in Python, all booleans are also integers (1 and 0 for true and false, respectively) |
211
+
| Number | Behaves like Python numbers. Python only has one integral and one floating point type, but it cares about the ranges in some places such as typed arrays. |
212
+
| String | Behaves like Python strings. |
213
+
| Buffer | Buffers are also a concept in Python's native API (albeit a bit different). Interop buffers are treated like Python buffers in some places (like `memoryview`) to avoid copies of data. |
214
+
| Array | Arrays can be used with subscript access like Python lists, with integers and slices as indices. |
215
+
| Hash | Hashes can be used with subscript access like Python dicts, with any hashable kind of object as key. "Hashable" follows Python semantics, generally all interop types with identity are deemed "hashable". Note that if an interop object is both Array and Hash, the behavior of the subscript access is undefined. |
216
+
| Members | Members can be read using normal Python ~.~ notation or the `getattr` etc functions. |
217
+
| Iterable | Iterables are treated like Python objects with an `__iter__` method, that is, they can be used in loops and other places that accept Python iterables. |
218
+
| Iterator | Iterators are treated like Python objects with a `__next__` method. |
219
+
| Exception | Interop exceptions can be caught in generic except clauses. |
220
+
| MetaObject | Interop meta objects can be used in subtype and isinstance checks |
221
+
| Executable | Executable objects can be executed as functions, but never with keyword arguments. |
222
+
| Instantiable | Instantiable objects behave like executable objects (similar to how Python treats this) |
0 commit comments