Package jep.python
Class PyCallable
java.lang.Object
jep.JepAccess
jep.python.PyObject
jep.python.PyCallable
- All Implemented Interfaces:
AutoCloseable
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 Summary
Modifier and TypeMethodDescriptionInvokes this callable with the args in order.Invokes this callable with positional args and keyword args.Invokes this callable with keyword args.<T> T
Invokes this callable with the args in order, converting the return value to the given class.<T> T
Invokes this callable with positional args and keyword args, converting the return value to the given class.<T> T
Invokes this callable with keyword args, converting the return value to the given class.
-
Method Details
-
call
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
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 invocationargs
- args to pass to the function in order- Returns:
- a value of the given type
- Throws:
JepException
- if an error occurs
-
call
Invokes this callable with keyword args.- Parameters:
kwargs
- a Map of keyword args- Returns:
- an
Object
value - Throws:
JepException
- if an error occurs
-
callAs
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 invocationkwargs
- a Map of keyword args- Returns:
- a value of the given type
- Throws:
JepException
- if an error occurs
-
call
Invokes this callable with positional args and keyword args.- Parameters:
args
- args to pass to the function in orderkwargs
- 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 JepExceptionInvokes 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 invocationargs
- args to pass to the function in orderkwargs
- a Map of keyword args- Returns:
- a value of the given type
- Throws:
JepException
- if an error occurs
-