ObjectProtocol

Trait ObjectProtocol 

Source
pub trait ObjectProtocol: PythonObject {
Show 18 methods // Provided methods fn hasattr<N>(&self, py: Python<'_>, attr_name: N) -> PyResult<bool> where N: ToPyObject { ... } fn getattr<N>(&self, py: Python<'_>, attr_name: N) -> PyResult<PyObject> where N: ToPyObject { ... } fn setattr<N, V>( &self, py: Python<'_>, attr_name: N, value: V, ) -> PyResult<()> where N: ToPyObject, V: ToPyObject { ... } fn delattr<N>(&self, py: Python<'_>, attr_name: N) -> PyResult<()> where N: ToPyObject { ... } fn compare<O>(&self, py: Python<'_>, other: O) -> PyResult<Ordering> where O: ToPyObject { ... } fn rich_compare<O>( &self, py: Python<'_>, other: O, compare_op: CompareOp, ) -> PyResult<PyObject> where O: ToPyObject { ... } fn repr(&self, py: Python<'_>) -> PyResult<PyString> { ... } fn str(&self, py: Python<'_>) -> PyResult<PyString> { ... } fn is_callable(&self, _py: Python<'_>) -> bool { ... } fn call<A>( &self, py: Python<'_>, args: A, kwargs: Option<&PyDict>, ) -> PyResult<PyObject> where A: ToPyObject<ObjectType = PyTuple> { ... } fn call_method<A>( &self, py: Python<'_>, name: &str, args: A, kwargs: Option<&PyDict>, ) -> PyResult<PyObject> where A: ToPyObject<ObjectType = PyTuple> { ... } fn hash(&self, py: Python<'_>) -> PyResult<Py_hash_t> { ... } fn is_true(&self, py: Python<'_>) -> PyResult<bool> { ... } fn len(&self, py: Python<'_>) -> PyResult<usize> { ... } fn get_item<K>(&self, py: Python<'_>, key: K) -> PyResult<PyObject> where K: ToPyObject { ... } fn set_item<K, V>(&self, py: Python<'_>, key: K, value: V) -> PyResult<()> where K: ToPyObject, V: ToPyObject { ... } fn del_item<K>(&self, py: Python<'_>, key: K) -> PyResult<()> where K: ToPyObject { ... } fn iter<'p>(&self, py: Python<'p>) -> PyResult<PyIterator<'p>> { ... }
}
Expand description

Trait that contains methods

Provided Methods§

Source

fn hasattr<N>(&self, py: Python<'_>, attr_name: N) -> PyResult<bool>
where N: ToPyObject,

Determines whether this object has the given attribute. This is equivalent to the Python expression ‘hasattr(self, attr_name)’.

Source

fn getattr<N>(&self, py: Python<'_>, attr_name: N) -> PyResult<PyObject>
where N: ToPyObject,

Retrieves an attribute value. This is equivalent to the Python expression ‘self.attr_name’.

Source

fn setattr<N, V>(&self, py: Python<'_>, attr_name: N, value: V) -> PyResult<()>
where N: ToPyObject, V: ToPyObject,

Sets an attribute value. This is equivalent to the Python expression ‘self.attr_name = value’.

Source

fn delattr<N>(&self, py: Python<'_>, attr_name: N) -> PyResult<()>
where N: ToPyObject,

Deletes an attribute. This is equivalent to the Python expression ‘del self.attr_name’.

Source

fn compare<O>(&self, py: Python<'_>, other: O) -> PyResult<Ordering>
where O: ToPyObject,

Compares two Python objects.

On Python 2, this is equivalent to the Python expression ‘cmp(self, other)’.

On Python 3, 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")
Source

fn rich_compare<O>( &self, py: Python<'_>, other: O, compare_op: CompareOp, ) -> PyResult<PyObject>
where O: ToPyObject,

Compares two Python objects.

Depending on the value of compare_op, 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
Source

fn repr(&self, py: Python<'_>) -> PyResult<PyString>

Compute the string representation of self. This is equivalent to the Python expression ‘repr(self)’.

Source

fn str(&self, py: Python<'_>) -> PyResult<PyString>

Compute the string representation of self. This is equivalent to the Python expression ‘str(self)’.

Source

fn is_callable(&self, _py: Python<'_>) -> bool

Determines whether this object is callable.

Source

fn call<A>( &self, py: Python<'_>, args: A, kwargs: Option<&PyDict>, ) -> PyResult<PyObject>
where A: ToPyObject<ObjectType = PyTuple>,

Calls the object. This is equivalent to the Python expression: ‘self(*args, **kwargs)’

args should be a value that, when converted to Python, results in a tuple. For this purpose, you can use:

  • cpython::NoArgs when calling a method without any arguments
  • otherwise, a Rust tuple with 1 or more elements
Source

fn call_method<A>( &self, py: Python<'_>, name: &str, args: A, kwargs: Option<&PyDict>, ) -> PyResult<PyObject>
where A: ToPyObject<ObjectType = PyTuple>,

Calls a method on the object. This is equivalent to the Python expression: ‘self.name(*args, **kwargs)’

args should be a value that, when converted to Python, results in a tuple. For this purpose, you can use:

  • cpython::NoArgs when calling a method without any arguments
  • otherwise, a Rust tuple with 1 or more elements
§Example
use cpython::{NoArgs, ObjectProtocol};
// Call method without arguments:
let value = obj.call_method(py, "method0", NoArgs, None).unwrap();
// Call method with a single argument:
obj.call_method(py, "method1", (true,), None).unwrap();
Source

fn hash(&self, py: Python<'_>) -> PyResult<Py_hash_t>

Retrieves the hash code of the object. This is equivalent to the Python expression: ‘hash(self)’

Source

fn is_true(&self, py: Python<'_>) -> PyResult<bool>

Returns whether the object is considered to be true. This is equivalent to the Python expression: ‘not not self’

Source

fn len(&self, py: Python<'_>) -> PyResult<usize>

Returns the length of the sequence or mapping. This is equivalent to the Python expression: ‘len(self)’

Source

fn get_item<K>(&self, py: Python<'_>, key: K) -> PyResult<PyObject>
where K: ToPyObject,

This is equivalent to the Python expression: ‘self[key]’

Source

fn set_item<K, V>(&self, py: Python<'_>, key: K, value: V) -> PyResult<()>
where K: ToPyObject, V: ToPyObject,

Sets an item value. This is equivalent to the Python expression ‘self[key] = value’.

Source

fn del_item<K>(&self, py: Python<'_>, key: K) -> PyResult<()>
where K: ToPyObject,

Deletes an item. This is equivalent to the Python expression ‘del self[key]’.

Source

fn iter<'p>(&self, py: Python<'p>) -> PyResult<PyIterator<'p>>

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.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§