Skip to main content

Overview

The Object Protocol provides generic operations that work with any Python object through the abstract object interface. These functions operate on PyObject* pointers.

Type Checking

PyObject_Type

Return the type object for object o. Equivalent to type(o) in Python.
PyObject*
required
Object to get the type of
Returns: New reference to the type object, or NULL on error

PyObject_TypeCheck

Check if object o is of type type or a subtype. Returns: 1 if true, 0 if false Example:

PyObject_IsInstance

Check if inst is an instance of class cls. Equivalent to isinstance(inst, cls). Returns: 1 for true, 0 for false, -1 on error

Attribute Access

PyObject_HasAttr

Check if object has an attribute. Equivalent to hasattr(o, attr_name).
PyObject*
required
Object to check
PyObject*
required
Attribute name (string object)
Returns: 1 if attribute exists, 0 otherwise

PyObject_GetAttr

Get an attribute from an object. Equivalent to o.attr_name or getattr(o, attr_name). Returns: New reference to attribute value, or NULL on error Example:

PyObject_GetAttrString

Convenience function for getting attributes with C string names. Example:

PyObject_SetAttr

Set an attribute. Equivalent to o.attr_name = v or setattr(o, attr_name, v).
PyObject*
required
Object to modify
PyObject*
required
Attribute name (string object)
PyObject*
required
New attribute value
Returns: 0 on success, -1 on error

PyObject_DelAttr

Delete an attribute. Equivalent to del o.attr_name. Returns: 0 on success, -1 on error

Rich Comparison

PyObject_RichCompare

Compare two objects with a comparison operator.
PyObject*
required
Left operand
PyObject*
required
Right operand
int
required
Comparison operator: Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, Py_GE
Returns: Comparison result (new reference), or NULL on error Example:

PyObject_RichCompareBool

Convenience function returning boolean result directly. Returns: 1 for true, 0 for false, -1 on error

Callable Objects

PyCallable_Check

Check if object is callable. Returns: 1 if callable, 0 otherwise (never fails)

PyObject_Call

Call a callable object.
PyObject*
required
Callable object (function, method, class, etc.)
PyObject*
required
Tuple of positional arguments (can be empty tuple)
PyObject*
Dictionary of keyword arguments (can be NULL)
Returns: Return value (new reference), or NULL on error Example:

PyObject_CallObject

Call with only positional arguments. Example:

PyObject_CallFunction

Call with C arguments built using format string. Example:

PyObject_CallMethod

Call a method on an object. Example:

Item Access

PyObject_GetItem

Get item from mapping or sequence. Equivalent to o[key].
PyObject*
required
Container object
PyObject*
required
Key or index
Returns: New reference to item, or NULL on error Example:

PyObject_SetItem

Set item in container. Equivalent to o[key] = value. Returns: 0 on success, -1 on error

PyObject_DelItem

Delete item from container. Equivalent to del o[key]. Returns: 0 on success, -1 on error

Sequence Protocol

PyObject_Length / PyObject_Size

Get length of sequence or mapping. Equivalent to len(o). Returns: Length, or -1 on error

PyObject_GetIter

Get iterator for object. Equivalent to iter(o). Returns: New reference to iterator, or NULL on error Example:

Truth Value Testing

PyObject_IsTrue

Test truth value. Equivalent to bool(o). Returns: 1 for true, 0 for false, -1 on error

PyObject_Not

Logical negation. Equivalent to not o. Returns: 1 if false, 0 if true, -1 on error

String Representation

PyObject_Repr

Get object representation. Equivalent to repr(o). Returns: New reference to string, or NULL on error

PyObject_Str

Get string representation. Equivalent to str(o). Returns: New reference to string, or NULL on error

PyObject_ASCII

Get ASCII representation. Equivalent to ascii(o). Returns: New reference to string with non-ASCII characters escaped

Hash Values

PyObject_Hash

Compute hash value. Equivalent to hash(o). Returns: Hash value, or -1 on error
Don’t confuse return value -1 with hash value -1. Check PyErr_Occurred() on -1 return.

PyObject_HashNotImplemented

Helper to raise TypeError for unhashable types. Returns: Always -1 and sets exception

Reference Counting Helpers

Py_Is

Test identity. Equivalent to x is y. Returns: 1 if same object, 0 otherwise

Py_IsNone

Test if object is None. Equivalent to x is None.

Py_IsTrue / Py_IsFalse

Test if object is True or False singleton.

Memory and Copying

PyObject_Bytes

Get bytes representation. Equivalent to bytes(o). Returns: New reference to bytes object, or NULL on error

Complete Example

See Also

Type Objects

Define and work with type objects

Reference Counting

Manage object lifetimes

Concrete Objects

Type-specific operations

Exception Handling

Handle errors in object operations