#[repr(C)]
pub struct PyObject { /* private fields */ }
Expand description

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.

Implementations

Creates a PyObject instance for the given FFI pointer. This moves ownership over the pointer into the PyObject. Undefined behavior if the pointer is NULL or invalid.

Creates a PyObject instance for the given FFI pointer. Calls Py_INCREF() on the ptr. Undefined behavior if the pointer is NULL or invalid.

Creates a PyObject instance for the given FFI pointer. This moves ownership over the pointer into the PyObject. Returns None for null pointers; undefined behavior if the pointer is invalid.

Returns None for null pointers; undefined behavior if the pointer is invalid.

Gets the underlying FFI pointer. Returns a borrowed pointer.

Gets the underlying FFI pointer. Consumes self without calling Py_DECREF(), thus returning an owned pointer.

Transmutes an FFI pointer to &PyObject. Undefined behavior if the pointer is NULL or invalid.

Transmutes a slice of owned FFI pointers to &[PyObject]. Undefined behavior if any pointer in the slice is NULL or invalid.

Gets the reference count of this Python object.

Gets the Python type object for this object’s type.

Casts the PyObject to a concrete Python object type. Causes undefined behavior if the object is not of the expected type. This is a wrapper function around PythonObject::unchecked_downcast_from().

Casts the PyObject to a concrete Python object type. Fails with PythonObjectDowncastError if the object is not of the expected type. This is a wrapper function around PythonObjectWithCheckedDowncast::downcast_from().

Casts the PyObject to a concrete Python object type. Causes undefined behavior if the object is not of the expected type. This is a wrapper function around PythonObject::unchecked_downcast_borrow_from().

Casts the PyObject to a concrete Python object type. Fails with PythonObjectDowncastError if the object is not of the expected type. This is a wrapper function around PythonObjectWithCheckedDowncast::downcast_borrow_from().

Extracts some type from the Python object. This is a wrapper function around FromPyObject::from_py_object().

True if this is None in Python.

Trait Implementations

Gets the size of the object, in bytes.
Allocates a new object (usually by calling ty->tp_alloc), and initializes it using init_val. ty must be derived from the Self type, and the resulting object must be of type ty. Read more
Calls the rust destructor for the object and frees the memory (usually by calling ptr->ob_type->tp_free). This function is used as tp_dealloc implementation. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more

Dropping a PyObject decrements the reference count on the object by 1.

Executes the destructor for this type. Read more
Extracts Self from the source PyObject.
Extracts Self from the source PyObject.
Perform addition (self + other) Read more
Perform subtraction (self - other) Read more
Perform multiplication (self * other) Read more
Perform matrix multiplication, equivalent to the Python expression self @ other Read more
Perform exponentiation, equivalent to the Python expression self ** other, or the two-argument form of the builtin method pow: pow(self, other) Read more
Perform exponentiation modulo an integer, mathematically equivalent to self ** other % mod but computed much more efficiently. Read more
Perform the “true division” operation, equivalent to the Python expression self / other, Read more
Perform the “floor division” operation, equivalent to the Python expression self // other, Read more
Return the remainder of dividing self by other, equivalent to the Python expression self % other Read more
Perform combined division and modulo, equivalent to the builtin method divmod(self, other) Read more
Perform the negation of self (-self) Read more
Invoke the ‘positive’ operation, equivalent to the Python expression +self Read more
Return the absolute value of self, equivalent to calling the builtin function abs Read more
Perform the bitwise negation of self, equivalent to the Python expression ~self Read more
Shift this value to the left by the specified number of bits, equivalent to the Python expression self << bits Read more
Shift this value to the right by the specified number of bits, equivalent to the Python expression self >> bits Read more
Perform the “bitwise and” of self & other Read more
Perform the “bitwise exclusive or”, equivalent to Python expression self ^ other Read more
Perform the “bitwise or” of self | other Read more
Convert this object to an integer, equivalent to the builtin function int(self) Read more
Convert this object to a float, equivalent to the builtin function float(self) Read more
Losslessly convert this object to an integer index, as if calling operator.index() Read more
Determines whether this object has the given attribute. This is equivalent to the Python expression ‘hasattr(self, attr_name)’. Read more
Retrieves an attribute value. This is equivalent to the Python expression ‘self.attr_name’. Read more
Sets an attribute value. This is equivalent to the Python expression ‘self.attr_name = value’. Read more
Deletes an attribute. This is equivalent to the Python expression ‘del self.attr_name’. Read more
Compares two Python objects. Read more
Compares two Python objects. Read more
Compute the string representation of self. This is equivalent to the Python expression ‘repr(self)’. Read more
Compute the string representation of self. This is equivalent to the Python expression ‘str(self)’. Read more
Determines whether this object is callable.
Calls the object. This is equivalent to the Python expression: ‘self(*args, **kwargs)’ Read more
Calls a method on the object. This is equivalent to the Python expression: ‘self.name(*args, **kwargs)’ Read more
Retrieves the hash code of the object. This is equivalent to the Python expression: ‘hash(self)’ Read more
Returns whether the object is considered to be true. This is equivalent to the Python expression: ‘not not self’ Read more
Returns the length of the sequence or mapping. This is equivalent to the Python expression: ‘len(self)’ Read more
This is equivalent to the Python expression: ‘self[key]’
Sets an item value. This is equivalent to the Python expression ‘self[key] = value’. Read more
Deletes an item. This is equivalent to the Python expression ‘del self[key]’. Read more
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

PyObject implements the == operator using reference equality: obj1 == obj2 in rust is equivalent to obj1 is obj2 in Python.

This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
Casts the Python object to PyObject.
Casts the Python object to PyObject.
Unchecked downcast from PyObject to Self. Undefined behavior if the input object does not have the expected type. Read more
Unchecked downcast from PyObject to Self. Undefined behavior if the input object does not have the expected type. Read more
Cast from PyObject to a concrete Python object type.
Cast from PyObject to a concrete Python object type.
Retrieves the type object for this Python object type.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Converts self into a Python object.
Converts self into a Python object. Read more
Converts self into a Python object and calls the specified closure on the native FFI pointer underlying the Python object. Read more

PyObject implements the == operator using reference equality: obj1 == obj2 in rust is equivalent to obj1 is obj2 in Python.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.