[−][src]Trait pyo3::ObjectProtocol
Python object model helper methods
Required methods
fn hasattr<N>(&self, attr_name: N) -> PyResult<bool> where
N: ToPyObject,
N: ToPyObject,
Determines whether this object has the given attribute.
This is equivalent to the Python expression hasattr(self, attr_name).
fn getattr<N>(&self, attr_name: N) -> PyResult<&PyAny> where
N: ToPyObject,
N: ToPyObject,
Retrieves an attribute value.
This is equivalent to the Python expression self.attr_name.
fn setattr<N, V>(&self, attr_name: N, value: V) -> PyResult<()> where
N: ToBorrowedObject,
V: ToBorrowedObject,
N: ToBorrowedObject,
V: ToBorrowedObject,
Sets an attribute value.
This is equivalent to the Python expression self.attr_name = value.
fn delattr<N>(&self, attr_name: N) -> PyResult<()> where
N: ToPyObject,
N: ToPyObject,
Deletes an attribute.
This is equivalent to the Python expression del self.attr_name.
fn compare<O>(&self, other: O) -> PyResult<Ordering> where
O: ToPyObject,
O: ToPyObject,
Compares two Python objects.
This is equivalent to:
if self == other:
return Equal
elif a < b:
return Less
elif a > b:
return Greater
else:
raise TypeError("ObjectProtocol::compare(): All comparisons returned false")
fn rich_compare<O>(&self, other: O, compare_op: CompareOp) -> PyResult<PyObject> where
O: ToPyObject,
O: ToPyObject,
Compares two Python objects.
Depending on the value of compare_op, this is equivalent to one of the
following Python expressions:
- CompareOp::Eq:
self == other - CompareOp::Ne:
self != other - CompareOp::Lt:
self < other - CompareOp::Le:
self <= other - CompareOp::Gt:
self > other - CompareOp::Ge:
self >= other
fn repr(&self) -> PyResult<&PyString>
Computes the "repr" representation of self.
This is equivalent to the Python expression repr(self).
fn str(&self) -> PyResult<&PyString>
Computes the "str" representation of self.
This is equivalent to the Python expression str(self).
fn is_callable(&self) -> bool
Determines whether this object is callable.
fn call(
&self,
args: impl IntoPy<Py<PyTuple>>,
kwargs: Option<&PyDict>
) -> PyResult<&PyAny>
&self,
args: impl IntoPy<Py<PyTuple>>,
kwargs: Option<&PyDict>
) -> PyResult<&PyAny>
Calls the object.
This is equivalent to the Python expression self(*args, **kwargs).
fn call1(&self, args: impl IntoPy<Py<PyTuple>>) -> PyResult<&PyAny>
Calls the object with only positional arguments.
This is equivalent to the Python expression self(*args).
fn call0(&self) -> PyResult<&PyAny>
Calls the object without arguments.
This is equivalent to the Python expression self().
fn call_method(
&self,
name: &str,
args: impl IntoPy<Py<PyTuple>>,
kwargs: Option<&PyDict>
) -> PyResult<&PyAny>
&self,
name: &str,
args: impl IntoPy<Py<PyTuple>>,
kwargs: Option<&PyDict>
) -> PyResult<&PyAny>
Calls a method on the object.
This is equivalent to the Python expression self.name(*args, **kwargs).
Example
use pyo3::types::IntoPyDict; let gil = Python::acquire_gil(); let py = gil.python(); let list = vec![3, 6, 5, 4, 7].to_object(py); let dict = vec![("reverse", true)].into_py_dict(py); list.call_method(py, "sort", (), Some(dict)).unwrap(); assert_eq!(list.extract::<Vec<i32>>(py).unwrap(), vec![7, 6, 5, 4, 3]);
fn call_method1(
&self,
name: &str,
args: impl IntoPy<Py<PyTuple>>
) -> PyResult<&PyAny>
&self,
name: &str,
args: impl IntoPy<Py<PyTuple>>
) -> PyResult<&PyAny>
Calls a method on the object with only positional arguments.
This is equivalent to the Python expression self.name(*args).
fn call_method0(&self, name: &str) -> PyResult<&PyAny>
Calls a method on the object without arguments.
This is equivalent to the Python expression self.name().
fn hash(&self) -> PyResult<isize>
Retrieves the hash code of the object.
This is equivalent to the Python expression hash(self).
fn is_true(&self) -> PyResult<bool>
Returns whether the object is considered to be true.
This is equivalent to the Python expression bool(self).
fn is_none(&self) -> bool
Returns whether the object is considered to be None.
This is equivalent to the Python expression self is None.
fn len(&self) -> PyResult<usize>
Returns the length of the sequence or mapping.
This is equivalent to the Python expression len(self).
fn is_empty(&self) -> PyResult<bool>
Returns true if the sequence or mapping has a length of 0.
This is equivalent to the Python expression len(self) == 0.
fn get_item<K>(&self, key: K) -> PyResult<&PyAny> where
K: ToBorrowedObject,
K: ToBorrowedObject,
Gets an item from the collections.
This is equivalent to the Python expression self[key].
fn set_item<K, V>(&self, key: K, value: V) -> PyResult<()> where
K: ToBorrowedObject,
V: ToBorrowedObject,
K: ToBorrowedObject,
V: ToBorrowedObject,
Sets a collection item value.
This is equivalent to the Python expression self[key] = value.
fn del_item<K>(&self, key: K) -> PyResult<()> where
K: ToBorrowedObject,
K: ToBorrowedObject,
Deletes an item from the collection.
This is equivalent to the Python expression del self[key].
fn iter(&self) -> PyResult<PyIterator>
Takes an object and returns an iterator for it.
This is typically a new iterator but if the argument is an iterator, this returns itself.
fn get_type(&self) -> &PyType
Returns the Python type object for this object's type.
fn get_type_ptr(&self) -> *mut PyTypeObject
Returns the Python type pointer for this object.
fn cast_as<'a, D>(&'a self) -> Result<&'a D, PyDowncastError> where
D: PyTryFrom<'a>,
&'a PyAny: From<&'a Self>,
D: PyTryFrom<'a>,
&'a PyAny: From<&'a Self>,
Casts the PyObject to a concrete Python object type.
This can cast only to native Python types, not types implemented in Rust.
fn extract<'a, D>(&'a self) -> PyResult<D> where
D: FromPyObject<'a>,
&'a PyAny: From<&'a Self>,
D: FromPyObject<'a>,
&'a PyAny: From<&'a Self>,
Extracts some type from the Python object.
This is a wrapper function around FromPyObject::extract().
fn get_refcnt(&self) -> isize
Returns the reference count for the Python object.
fn None(&self) -> PyObject
Gets the Python builtin value None.
Implementors
impl<T> ObjectProtocol for T where
T: PyNativeType + AsPyPointer, [src]
T: PyNativeType + AsPyPointer,
fn hasattr<N>(&self, attr_name: N) -> PyResult<bool> where
N: ToPyObject, [src]
N: ToPyObject,
fn getattr<N>(&self, attr_name: N) -> PyResult<&PyAny> where
N: ToPyObject, [src]
N: ToPyObject,
fn setattr<N, V>(&self, attr_name: N, value: V) -> PyResult<()> where
N: ToBorrowedObject,
V: ToBorrowedObject, [src]
N: ToBorrowedObject,
V: ToBorrowedObject,
fn delattr<N>(&self, attr_name: N) -> PyResult<()> where
N: ToPyObject, [src]
N: ToPyObject,
fn compare<O>(&self, other: O) -> PyResult<Ordering> where
O: ToPyObject, [src]
O: ToPyObject,
fn rich_compare<O>(&self, other: O, compare_op: CompareOp) -> PyResult<PyObject> where
O: ToPyObject, [src]
O: ToPyObject,
fn repr(&self) -> PyResult<&PyString>[src]
fn str(&self) -> PyResult<&PyString>[src]
fn is_callable(&self) -> bool[src]
fn call(
&self,
args: impl IntoPy<Py<PyTuple>>,
kwargs: Option<&PyDict>
) -> PyResult<&PyAny>[src]
&self,
args: impl IntoPy<Py<PyTuple>>,
kwargs: Option<&PyDict>
) -> PyResult<&PyAny>
fn call0(&self) -> PyResult<&PyAny>[src]
fn call1(&self, args: impl IntoPy<Py<PyTuple>>) -> PyResult<&PyAny>[src]
fn call_method(
&self,
name: &str,
args: impl IntoPy<Py<PyTuple>>,
kwargs: Option<&PyDict>
) -> PyResult<&PyAny>[src]
&self,
name: &str,
args: impl IntoPy<Py<PyTuple>>,
kwargs: Option<&PyDict>
) -> PyResult<&PyAny>
fn call_method0(&self, name: &str) -> PyResult<&PyAny>[src]
fn call_method1(
&self,
name: &str,
args: impl IntoPy<Py<PyTuple>>
) -> PyResult<&PyAny>[src]
&self,
name: &str,
args: impl IntoPy<Py<PyTuple>>
) -> PyResult<&PyAny>
fn hash(&self) -> PyResult<isize>[src]
fn is_true(&self) -> PyResult<bool>[src]
fn is_none(&self) -> bool[src]
fn len(&self) -> PyResult<usize>[src]
fn is_empty(&self) -> PyResult<bool>[src]
fn get_item<K>(&self, key: K) -> PyResult<&PyAny> where
K: ToBorrowedObject, [src]
K: ToBorrowedObject,
fn set_item<K, V>(&self, key: K, value: V) -> PyResult<()> where
K: ToBorrowedObject,
V: ToBorrowedObject, [src]
K: ToBorrowedObject,
V: ToBorrowedObject,
fn del_item<K>(&self, key: K) -> PyResult<()> where
K: ToBorrowedObject, [src]
K: ToBorrowedObject,
fn iter(&self) -> PyResult<PyIterator>[src]
fn get_type(&self) -> &PyType[src]
fn get_type_ptr(&self) -> *mut PyTypeObject[src]
fn cast_as<'a, D>(&'a self) -> Result<&'a D, PyDowncastError> where
D: PyTryFrom<'a>,
&'a PyAny: From<&'a Self>, [src]
D: PyTryFrom<'a>,
&'a PyAny: From<&'a Self>,
fn extract<'a, D>(&'a self) -> PyResult<D> where
D: FromPyObject<'a>,
&'a PyAny: From<&'a T>, [src]
D: FromPyObject<'a>,
&'a PyAny: From<&'a T>,