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 |
---|---|
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()
Attempts to remove 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()
Returns a
Set view of the mappings contained in this map. |
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() . |
<T> T |
toJava(Class<T> klass)
Attempts to view the Python object as the given Java type.
|
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 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()
Attempts to remove 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 manually: 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.
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>
key
- key whose presence in this map is to be testedpublic 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>
o
- value whose presence in this map is to be testedpublic Set<Map.Entry<String,PyObject>> entrySet()
java.util.Map
Set
view of the mappings contained in this map.
The set is backed by the map, so changes to the map are
reflected in the set, and vice-versa. If the map is modified
while an iteration over the set is in progress (except through
the iterator's own remove operation, or through the
setValue operation on a map entry returned by the
iterator) the results of the iteration are undefined. The set
supports element removal, which removes the corresponding
mapping from the map, via the Iterator.remove,
Set.remove, removeAll, retainAll and
clear operations. It does not support the
add or addAll operations.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.Object.hashCode()
,
HashMap
protected void finalize() throws Throwable
Calls close()
.
finalize
in class Object
Throwable
- the Exception
raised by this methodWeakReference
,
PhantomReference
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 manually: it will be called automatically by the methods of this class which take Object
parameters.
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()
.
hashCode
in interface Map<String,PyObject>
hashCode
in class AbstractMap<String,PyObject>
Map.Entry.hashCode()
,
Object.equals(Object)
,
Set.equals(Object)
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 put(String key, PyObject value)
Equivalent to Python setattr()
.
put
in interface Map<String,PyObject>
put
in class AbstractMap<String,PyObject>
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keypublic 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,
public String repr()
Equivalent to Python repr()
.
public <T> T toJava(Class<T> klass)
Attempts to view the Python object as the given Java type. For example. toJava(String.class)
will attempt to view the object as a String.
Boolean
, Integer
or String
, and the Python object is of a compatible type, an equivalent object will be returned.ClassCastException
will be thrown.public String toString()
Equivalent to Python str()
.
toString
in class AbstractMap<String,PyObject>
public PyObject type()
Equivalent to Python type()
.