Package jep.python
Class PyObject
java.lang.Object
jep.JepAccess
jep.python.PyObject
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
PyCallable
A Java object that wraps a pointer to a Python object.
This class is not thread safe and PyObjects can only be used on the Thread
where they were created. When an Interpreter instance is closed all PyObjects
from that instance will be invalid and can no longer be used.
-
Method Summary
Modifier and TypeMethodDescription<T> T
Attempt to convert this object to a Java equivalant using the builtin Jep conversions.void
close()
void
Deletes an attribute on the wrapped Python object, similar to the Python built-in function delattr.boolean
Checks that the Java type matches and if so then uses Python's rich compare with the == operator to check if this wrapped Python object matches the other PyObject.Access an attribute of the wrapped Python Object, similar to the Python built-in function getattr.<T> T
Access an attribute of the wrapped Python Object, similar to the Python built-in function getattr.int
hashCode()
Produces the hash code of the wrapped Python object by using the Python built-in method hash.void
isValid()
Deprecated.In a future release this method will not be public and/or its method signature may change.<T> T
Create a dynamic proxy that implements the provided interfaces by calling the corresponding Python methods on this PyObject.void
Sets an attribute on the wrapped Python object, similar to the Python built-in function setattr.toString()
Produces the string representation of the wrapped Python object by using the Python built-in method str.
-
Method Details
-
isValid
Deprecated.In a future release this method will not be public and/or its method signature may change.Check if PyObject is valid.- Throws:
JepException
- if it is not safe to use this python object
-
close
- Specified by:
close
in interfaceAutoCloseable
- Throws:
JepException
-
getAttr
Access an attribute of the wrapped Python Object, similar to the Python built-in function getattr. This is equivalent to the Python statementthis.attr_name
.- Parameters:
attr_name
- the attribute name- Returns:
- a Java version of the attribute
- Throws:
JepException
- if an error occurs- Since:
- 3.8
-
getAttr
Access an attribute of the wrapped Python Object, similar to the Python built-in function getattr. This method allows you to specify the return type, the supported types are the same asJep.getValue(String, Class)
.- Type Parameters:
T
- the generic type of the return type- Parameters:
attr_name
- the attribute nameclazz
- the Java class of the return type.- Returns:
- a Java version of the attribute
- Throws:
JepException
- if an error occurs- Since:
- 3.8
-
setAttr
Sets an attribute on the wrapped Python object, similar to the Python built-in function setattr. This is equivalent to the Python statementthis.attr_name = o
.- Parameters:
attr_name
- the attribute nameo
- the object to set as an attribute- Throws:
JepException
- if an error occurs- Since:
- 3.8
-
delAttr
Deletes an attribute on the wrapped Python object, similar to the Python built-in function delattr. This is equivalent to the Python statementdel this.attr_name
.- Parameters:
attr_name
- the name of the attribute to be deleted- Throws:
JepException
- if an error occurs- Since:
- 3.8
-
equals
Checks that the Java type matches and if so then uses Python's rich compare with the == operator to check if this wrapped Python object matches the other PyObject. Equals is not consistent between languages. Java is strict on equality while Python is flexible. For example in Python code: Integer.valueOf(5) == 5 will evaluate to True while in Java code: Integer.valueOf(5).equals(other) where other is a PyObject wrapping 5 will evaluate to false. -
toString
Produces the string representation of the wrapped Python object by using the Python built-in method str. -
hashCode
public int hashCode()Produces the hash code of the wrapped Python object by using the Python built-in method hash. Hash codes are not consistent between languages. For example the hash code of the string "hello" will be different in Python than in Java, even if this PyObject wrapped the string "hello". -
proxy
Create a dynamic proxy that implements the provided interfaces by calling the corresponding Python methods on this PyObject. This Python object must have methods matching those defined by the Java interfaces. Matching methods must have the same name, same number of arguments, and must return an object that can be converted to the correct return type. This method does not verify that this Python object has methods matching the Java interfaces. If a method is called on the proxy object that does not have a matching Python method a RuntimeException will be thrown. The returned proxy object can only be used when this PyObject is valid. It cannot be used on other threads or after the Interpreter that it originated from is closed.- Type Parameters:
T
- the generic type of the return type- Parameters:
primaryInterface
- A interface the returned object will implementextraInterfaces
- Optional additional interfaces the returned object will also implement.- Returns:
- a Java proxy implementing the provided interfaces.
- Throws:
JepException
- if an error occurs or the conversion is not possible- Since:
- 3.9
-
as
Attempt to convert this object to a Java equivalant using the builtin Jep conversions. The supported conversions are described inInterpreter.getValue(String, Class)
.- Type Parameters:
T
- the generic type of the return type- Parameters:
expectedType
- the Java class of the return type.- Returns:
- a Java version of this object
- Throws:
JepException
- if an error occurs or the conversion is not possible- Since:
- 3.9
-