Package jep.python
Class PyObject
- java.lang.Object
-
- jep.JepAccess
-
- jep.python.PyObject
-
- All Implemented Interfaces:
java.lang.AutoCloseable
- Direct Known Subclasses:
PyCallable
public class PyObject extends JepAccess implements java.lang.AutoCloseable
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
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description <T> T
as(java.lang.Class<T> expectedType)
Attempt to convert this object to a Java equivalant using the builtin Jep conversions.void
close()
void
delAttr(java.lang.String attr_name)
Deletes an attribute on the wrapped Python object, similar to the Python built-in function delattr.boolean
equals(java.lang.Object obj)
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.java.lang.Object
getAttr(java.lang.String attr_name)
Access an attribute of the wrapped Python Object, similar to the Python built-in function getattr.<T> T
getAttr(java.lang.String attr_name, java.lang.Class<T> clazz)
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
proxy(java.lang.Class<T> primaryInterface, java.lang.Class<?>... extraInterfaces)
Create a dynamic proxy that implements the provided interfaces by calling the corresponding Python methods on this PyObject.void
setAttr(java.lang.String attr_name, java.lang.Object o)
Sets an attribute on the wrapped Python object, similar to the Python built-in function setattr.java.lang.String
toString()
Produces the string representation of the wrapped Python object by using the Python built-in method str.
-
-
-
Method Detail
-
isValid
@Deprecated public void isValid() throws JepException
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
public void close() throws JepException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Throws:
JepException
-
getAttr
public java.lang.Object getAttr(java.lang.String attr_name) throws JepException
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
public <T> T getAttr(java.lang.String attr_name, java.lang.Class<T> clazz) throws JepException
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
public void setAttr(java.lang.String attr_name, java.lang.Object o) throws JepException
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
public void delAttr(java.lang.String attr_name) throws JepException
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
public boolean equals(java.lang.Object obj)
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.- Overrides:
equals
in classjava.lang.Object
-
toString
public java.lang.String toString()
Produces the string representation of the wrapped Python object by using the Python built-in method str.- Overrides:
toString
in classjava.lang.Object
-
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".- Overrides:
hashCode
in classjava.lang.Object
-
proxy
public <T> T proxy(java.lang.Class<T> primaryInterface, java.lang.Class<?>... extraInterfaces) throws JepException
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
public <T> T as(java.lang.Class<T> expectedType) throws JepException
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
-
-