Overview
The Object Protocol provides generic operations that work with any Python object through the abstract object interface. These functions operate onPyObject* pointers.
Type Checking
PyObject_Type
o. Equivalent to type(o) in Python.
PyObject*
required
Object to get the type of
NULL on error
PyObject_TypeCheck
o is of type type or a subtype.
Returns: 1 if true, 0 if false
Example:
PyObject_IsInstance
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
hasattr(o, attr_name).
PyObject*
required
Object to check
PyObject*
required
Attribute name (string object)
1 if attribute exists, 0 otherwise
PyObject_GetAttr
o.attr_name or getattr(o, attr_name).
Returns: New reference to attribute value, or NULL on error
Example:
PyObject_GetAttrString
PyObject_SetAttr
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
0 on success, -1 on error
PyObject_DelAttr
del o.attr_name.
Returns: 0 on success, -1 on error
Rich Comparison
PyObject_RichCompare
PyObject*
required
Left operand
PyObject*
required
Right operand
int
required
Comparison operator:
Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, Py_GENULL on error
Example:
PyObject_RichCompareBool
1 for true, 0 for false, -1 on error
Callable Objects
PyCallable_Check
1 if callable, 0 otherwise (never fails)
PyObject_Call
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)NULL on error
Example:
PyObject_CallObject
PyObject_CallFunction
PyObject_CallMethod
Item Access
PyObject_GetItem
o[key].
PyObject*
required
Container object
PyObject*
required
Key or index
NULL on error
Example:
PyObject_SetItem
o[key] = value.
Returns: 0 on success, -1 on error
PyObject_DelItem
del o[key].
Returns: 0 on success, -1 on error
Sequence Protocol
PyObject_Length / PyObject_Size
len(o).
Returns: Length, or -1 on error
PyObject_GetIter
iter(o).
Returns: New reference to iterator, or NULL on error
Example:
Truth Value Testing
PyObject_IsTrue
bool(o).
Returns: 1 for true, 0 for false, -1 on error
PyObject_Not
not o.
Returns: 1 if false, 0 if true, -1 on error
String Representation
PyObject_Repr
repr(o).
Returns: New reference to string, or NULL on error
PyObject_Str
str(o).
Returns: New reference to string, or NULL on error
PyObject_ASCII
ascii(o).
Returns: New reference to string with non-ASCII characters escaped
Hash Values
PyObject_Hash
hash(o).
Returns: Hash value, or -1 on error
PyObject_HashNotImplemented
TypeError for unhashable types.
Returns: Always -1 and sets exception
Reference Counting Helpers
Py_Is
x is y.
Returns: 1 if same object, 0 otherwise
Py_IsNone
None. Equivalent to x is None.
Py_IsTrue / Py_IsFalse
True or False singleton.
Memory and Copying
PyObject_Bytes
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
