Class PythonInterop

java.lang.Object
io.micronaut.context.python.PythonInterop

public final class PythonInterop extends Object
Interop helpers for Python code that calls Java APIs.

Python code imports the class like any other Java type:

from java.util.function import BiPredicate
from micronaut.context.python import PythonInterop

builder.exitCondition(PythonInterop.fn(BiPredicate, lambda scope, index: index == 3))
Since:
5.2.3
  • Method Details

    • fn

      public static <T> T fn(Class<T> type, org.graalvm.polyglot.Value callable)
      Adapts a Python callable to a functional interface.

      A Python callable passed to a Java method whose parameter is a standard functional interface is converted automatically, and when the method is overloaded on functional interfaces the overload whose interface method has the number of parameters of the callable is selected. This method adapts a callable to any functional interface explicitly, for overloads on custom functional interfaces or on interfaces of the same arity. The result is a host object that implements the interface: it keeps its Java class when it is returned to Python, whereas an automatically converted callable returns to Python as the original callable.

      Type Parameters:
      T - The interface type
      Parameters:
      type - The functional interface
      callable - The Python callable
      Returns:
      An implementation of the interface that invokes the callable
      Throws:
      IllegalArgumentException - if the type is not a functional interface or the value is not callable