public class PyObject extends AbstractMap<String,PyObject> implements AutoCloseable
Interface to a Python object.
None
is represented by Java null
. Other PyObject
s can be converted to their Java equivalents using toJava()
.Unless otherwise specified, all methods in this class throw PyException
on failure.
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
Modifier and Type | Method and Description |
---|---|
List<PyObject> |
asList()
Returns a view of the Python object as a list.
|
Map<PyObject,PyObject> |
asMap()
Returns a view of the Python object as a map.
|
Set<PyObject> |
asSet()
Returns a view of the Python object as a set.
|
PyObject |
call(Object... args)
Equivalent to Python
() syntax. |
PyObject |
callAttr(String key,
Object... args)
|
PyObject |
callAttrThrows(String key,
Object... args)
Same as
callAttr() , except that it directly passes any Java exception thrown by the Python code rather than wrapping it in a PyException |
PyObject |
callThrows(Object... args)
Same as
call() , except that it directly passes any Java exception thrown by the Python code rather than wrapping it in a PyException |
void |
clear()
Removes all attributes returned by
dir() . |
void |
close()
Releases the reference to the Python object.
|
boolean |
containsKey(Object key)
Equivalent to Python
hasattr() . |
boolean |
containsValue(Object o)
The value will be converted as described at
fromJava() . |
Set<Map.Entry<String,PyObject>> |
entrySet() |
boolean |
equals(Object that)
Equivalent to Python
== operator. |
protected void |
finalize()
Calls
close() . |
static PyObject |
fromJava(Object o)
Gives the given Java object a presence in the Python virtual machine.
|
PyObject |
get(Object key)
Equivalent to Python
getattr() . |
int |
hashCode()
Equivalent to Python
hash() . |
long |
id()
Equivalent to Python
id() . |
boolean |
isEmpty()
Equivalent to
. |
Set<String> |
keySet()
Equivalent to Python
dir() . |
PyObject |
put(String key,
Object value)
Equivalent to Python
setattr() . |
PyObject |
put(String key,
PyObject value)
Equivalent to Python
setattr() . |
PyObject |
remove(Object key)
Equivalent to Python
delattr() . |
String |
repr()
Equivalent to Python
repr() . |
boolean |
toBoolean()
Converts a Python
bool to a Java boolean . |
byte |
toByte()
Converts a Python
int to a Java byte . |
char |
toChar()
Converts a 1-character Python string to a Java
char . |
double |
toDouble()
Converts a Python
float or int to a Java double . |
float |
toFloat()
Converts a Python
float or int to a Java float . |
int |
toInt()
Converts a Python
int to a Java int . |
<T> T |
toJava(Class<T> klass)
Converts the Python object to the given Java type.
|
long |
toLong()
Converts a Python
int to a Java long . |
short |
toShort()
Converts a Python
int to a Java short . |
String |
toString()
Equivalent to Python
str() . |
PyObject |
type()
Equivalent to Python
type() . |
clone, putAll, size, values
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
public List<PyObject> asList()
Returns a view of the Python object as a list. The view is backed by the object, so changes to the object are reflected in the view, and vice-versa.
To add Java objects to the Python container through the view, first convert them using fromJava
.
UnsupportedOperationException
- if the Python object does not implement the methods __getitem__
and __len__
.public Map<PyObject,PyObject> asMap()
Returns a view of the Python object as a map. The view is backed by the object, so changes to the object are reflected in the view, and vice-versa.
PyObject already implements the Map
interface, but that is for attribute access (Python “.
” syntax, whereas the Map
returned by this method is for container access (Python “[]
” syntax).
To add Java objects to the Python container through the view, first convert them using fromJava
.
UnsupportedOperationException
- if the Python object does not implement the methods __contains__
, __getitem__
, __iter__
and __len__
.public Set<PyObject> asSet()
Returns a view of the Python object as a set. The view is backed by the object, so changes to the object are reflected in the view, and vice-versa.
To add Java objects to the Python container through the view, first convert them using fromJava
.
UnsupportedOperationException
- if the Python object does not implement the methods __contains__
, __iter__
and __len__
.public PyObject call(Object... args)
Equivalent to Python ()
syntax. Keyword arguments may be passed using instances of Kwarg
at the end of the parameter list. Parameters will be converted as described at fromJava()
.
public PyObject callAttrThrows(String key, Object... args) throws Throwable
Same as callAttr()
, except that it directly passes any Java exception thrown by the Python code rather than wrapping it in a PyException
Throwable
public PyObject callThrows(Object... args) throws Throwable
Same as call()
, except that it directly passes any Java exception thrown by the Python code rather than wrapping it in a PyException
Throwable
public void clear()
Removes all attributes returned by dir()
. Because dir()
usually includes non-removable attributes such as __class__
, this will probably fail unless the object has a custom __dir__
method.
public void close()
Releases the reference to the Python object. Unless the object represents an expensive resource, there’s no need to call this method directly: it will be called automatically when the PyObject is garbage-collected.
After calling close()
, the PyObject can no longer be used. If there are no other references to the underlying object, it may be destroyed by Python. If it continues to exist and is retrieved by Java code again, a new PyObject will be returned.
Caution: any references to the same Python object elsewhere in your program will be represented by the same PyObject, so they will all be invalidated by this call.
close
in interface AutoCloseable
public boolean containsKey(Object key)
Equivalent to Python hasattr()
.
containsKey
in interface Map<String,PyObject>
containsKey
in class AbstractMap<String,PyObject>
public boolean containsValue(Object o)
The value will be converted as described at fromJava()
.
containsValue
in interface Map<String,PyObject>
containsValue
in class AbstractMap<String,PyObject>
public boolean equals(Object that)
Equivalent to Python ==
operator.
equals
in interface Map<String,PyObject>
equals
in class AbstractMap<String,PyObject>
that
- Object to compare with this object. It will be converted as described at fromJava()
.true
if the given object is equal to this object.public static PyObject fromJava(Object o)
Gives the given Java object a presence in the Python virtual machine. There’s usually no need to call this method directly: it will be called automatically by the methods of this class which take Object
parameters.
For details of how Java objects are represented in Python, see the Python API.
public PyObject get(Object key)
Equivalent to Python getattr()
. In accordance with the Map
interface, when the attribute does not exist, this method returns null
rather than throwing an exception. To distinguish this from an attribute with a value of None
, use containsKey()
.
public int hashCode()
Equivalent to Python hash()
.
public long id()
Equivalent to Python id()
.
public boolean isEmpty()
public Set<String> keySet()
Equivalent to Python dir()
. The returned set is backed by the Python object, so changes to the object are reflected in the set, and vice-versa. If the object is modified while an iteration over the set is in progress (except through the iterator’s own remove
operation), the results of the iteration are undefined. The set supports element removal, but see the notes on remove()
. It does not support the add
or addAll
operations.
public PyObject put(String key, Object value)
Equivalent to Python setattr()
. The value will be converted as described at fromJava()
.
public PyObject remove(Object key)
Equivalent to Python delattr()
. This usually means it will only succeed in removing attributes of the object itself, even though dir()
also returns an object’s class attributes by default
In accordance with the Map
interface, when the attribute does not exist, this method returns null
rather than throwing an exception.
public String repr()
Equivalent to Python repr()
.
public boolean toBoolean()
Converts a Python bool
to a Java boolean
.
ClassCastException
- if the Python object is not of a compatible typepublic byte toByte()
Converts a Python int
to a Java byte
.
ClassCastException
- if the Python object is not of a compatible typepublic char toChar()
Converts a 1-character Python string to a Java char
.
ClassCastException
- if the Python object is not of a compatible typepublic double toDouble()
Converts a Python float
or int
to a Java double
.
ClassCastException
- if the Python object is not of a compatible typepublic float toFloat()
Converts a Python float
or int
to a Java float
.
ClassCastException
- if the Python object is not of a compatible typepublic int toInt()
Converts a Python int
to a Java int
.
ClassCastException
- if the Python object is not of a compatible typepublic <T> T toJava(Class<T> klass)
Converts the Python object to the given Java type.
If klass
is a primitive type (such as int
), or an immutable value type (such as Integer
or String
), and the Python object is compatible with it, an equivalent Java object will be returned. However, it’s more readable to use the type-specific methods like toInt()
, toString()
, etc.
If klass
is an array type, and the Python object is a sequence, then a copy of the sequence will be returned as a new array. In general, each element will be converted as if toJava
was called on it recursively. However, when converting a Python bytes
or bytearray
object to a Java byte[]
array, there is an unsigned-to-signed conversion: Python values 128 to 255 will be mapped to Java values -128 to -1.
If the Python object is a jclass or jarray object which is compatible with klass
, the underlying Java object will be returned.
Otherwise, a ClassCastException
will be thrown.
public long toLong()
Converts a Python int
to a Java long
.
ClassCastException
- if the Python object is not of a compatible typepublic short toShort()
Converts a Python int
to a Java short
.
ClassCastException
- if the Python object is not of a compatible typepublic String toString()
Equivalent to Python str()
.
toString
in class AbstractMap<String,PyObject>
public PyObject type()
Equivalent to Python type()
.