Package jep.python

Class PyCallable

All Implemented Interfaces:
AutoCloseable

public class PyCallable extends PyObject
A Java object that wraps a pointer to a Python callable. These objects can be instance methods, functions, lambdas, or any Python object implementing the __call__ method.

Instance Method Example:

 
     jep.exec("class Example(object):\n" +
              "    def __init__(self):\n" +
              "        pass\n" +
              "    def helloWorld(self):\n" +
              "        return 'Hello World'\n");
     jep.exec("instance = Example()");
     PyObject pyobj = jep.getValue("instance", PyObject.class);
     PyCallable pyHelloWorld = PyObject.getAttr("helloWorld", PyCallable.class);
     String result = (String) pyHelloWorld.call();
 
 

Function Example:

 
     jep.exec("def hello(arg):\n" +
              "    return 'Hello ' +  str(arg)");
     PyCallable pyHello = jep.getValue("hello", PyCallable.class);
     String result = (String) pyHello.call("World");
 
 
Since:
3.8
See Also:
  • Method Details

    • call

      public Object call(Object... args) throws JepException
      Invokes this callable with the args in order.
      Parameters:
      args - args to pass to the function in order
      Returns:
      an Object value
      Throws:
      JepException - if an error occurs
    • callAs

      public <T> T callAs(Class<T> expectedType, Object... args) throws JepException
      Invokes this callable with the args in order, converting the return value to the given class.
      Type Parameters:
      T - the generic type of the return type
      Parameters:
      expectedType - The expected return type of the invocation
      args - args to pass to the function in order
      Returns:
      a value of the given type
      Throws:
      JepException - if an error occurs
    • call

      public Object call(Map<String,Object> kwargs) throws JepException
      Invokes this callable with keyword args.
      Parameters:
      kwargs - a Map of keyword args
      Returns:
      an Object value
      Throws:
      JepException - if an error occurs
    • callAs

      public <T> T callAs(Class<T> expectedType, Map<String,Object> kwargs) throws JepException
      Invokes this callable with keyword args, converting the return value to the given class.
      Type Parameters:
      T - the generic type of the return type
      Parameters:
      expectedType - The expected return type of the invocation
      kwargs - a Map of keyword args
      Returns:
      a value of the given type
      Throws:
      JepException - if an error occurs
    • call

      public Object call(Object[] args, Map<String,Object> kwargs) throws JepException
      Invokes this callable with positional args and keyword args.
      Parameters:
      args - args to pass to the function in order
      kwargs - a Map of keyword args
      Returns:
      an Object value
      Throws:
      JepException - if an error occurs
    • callAs

      public <T> T callAs(Class<T> expectedType, Object[] args, Map<String,Object> kwargs) throws JepException
      Invokes this callable with positional args and keyword args, converting the return value to the given class.
      Type Parameters:
      T - the generic type of the return type
      Parameters:
      expectedType - The expected return type of the invocation
      args - args to pass to the function in order
      kwargs - a Map of keyword args
      Returns:
      a value of the given type
      Throws:
      JepException - if an error occurs