Struct cpython::PyObject [] [src]

pub struct PyObject {
    // some fields omitted
}

Represents a reference to a Python object.

Python objects are reference counted. Calling clone_ref() on a PyObject will return a new reference to the same object (thus incrementing the reference count). The Drop implementation will automatically decrement the reference count. You can also call release_ref() to explicitly decrement the reference count. This is slightly faster than relying on automatic drop, because release_ref does not need to check whether the GIL needs to be acquired.

PyObject can be used with all Python objects, since all python types derive from object. This crate also contains other, more specific types that serve as references to Python objects (e.g. PyTuple for Python tuples, etc.).

You can convert from any Python object to PyObject by calling as_object() or into_object() from the PythonObject trait. In the other direction, you can call cast_as() or cast_into() on PyObject to convert to more specific object types.

Most of the interesting methods are provided by the ObjectProtocol trait.

Trait Implementations

impl ObjectProtocol for PyObject
[src]

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)'. Read more

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'. Read more

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'. Read more

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'. Read more

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

Compute the string representation of self. This is equivalent to the Python expression 'repr(self)'. Read more

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

Compute the string representation of self. This is equivalent to the Python expression 'str(self)'. Read more

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

Determines whether this object is callable.

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)' Read more

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)' Read more

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)' Read more

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' Read more

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

Returns the length of the sequence or mapping. This is equivalent to the Python expression: 'len(self)' Read more

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

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

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'. Read more

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]'. Read more

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. Read more

impl Debug for PyObject
[src]

fn fmt(&self, f: &mut Formatter) -> Result<()Error>

Formats the value using the given formatter.

impl Display for PyObject
[src]

fn fmt(&self, f: &mut Formatter) -> Result<()Error>

Formats the value using the given formatter.