Package jep.python

Class PyObject

java.lang.Object
jep.JepAccess
jep.python.PyObject
All Implemented Interfaces:
AutoCloseable
Direct Known Subclasses:
PyCallable

public class PyObject extends JepAccess implements 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

    Modifier and Type
    Method
    Description
    <T> T
    as(Class<T> expectedType)
    Attempt to convert this object to a Java equivalant using the builtin Jep conversions.
    void
     
    void
    delAttr(String attr_name)
    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.
    getAttr(String attr_name)
    Access an attribute of the wrapped Python Object, similar to the Python built-in function getattr.
    <T> T
    getAttr(String attr_name, Class<T> clazz)
    Access an attribute of the wrapped Python Object, similar to the Python built-in function getattr.
    int
    Produces the hash code of the wrapped Python object by using the Python built-in method hash.
    void
    Deprecated.
    In a future release this method will not be public and/or its method signature may change.
    <T> T
    proxy(Class<T> primaryInterface, Class<?>... extraInterfaces)
    Create a dynamic proxy that implements the provided interfaces by calling the corresponding Python methods on this PyObject.
    void
    setAttr(String attr_name, Object o)
    Sets an attribute on the wrapped Python object, similar to the Python built-in function setattr.
    Produces the string representation of the wrapped Python object by using the Python built-in method str.

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • 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 interface AutoCloseable
      Throws:
      JepException
    • getAttr

      public Object getAttr(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 statement this.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(String attr_name, 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 as Jep.getValue(String, Class).
      Type Parameters:
      T - the generic type of the return type
      Parameters:
      attr_name - the attribute name
      clazz - 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(String attr_name, 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 statement this.attr_name = o.
      Parameters:
      attr_name - the attribute name
      o - the object to set as an attribute
      Throws:
      JepException - if an error occurs
      Since:
      3.8
    • delAttr

      public void delAttr(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 statement del 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(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 class Object
    • toString

      public String toString()
      Produces the string representation of the wrapped Python object by using the Python built-in method str.
      Overrides:
      toString in class 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 class Object
    • proxy

      public <T> T proxy(Class<T> primaryInterface, 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 implement
      extraInterfaces - 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(Class<T> expectedType) throws JepException
      Attempt to convert this object to a Java equivalant using the builtin Jep conversions. The supported conversions are described in Interpreter.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