7.2.2 Tuple Objects

PyTupleObject
This subtype of PyObject represents a Python tuple object.

PyTypeObject PyTuple_Type
This instance of PyTypeObject represents the Python tuple type.

int PyTuple_Check (PyObject *p)
Return true if the argument is a tuple object.

PyObject* PyTuple_New (int s)
Return a new tuple object of size s.

int PyTuple_Size (PyTupleObject *p)
Takes a pointer to a tuple object, and returns the size of that tuple.

PyObject* PyTuple_GetItem (PyTupleObject *p, int pos)
Returns the object at position pos in the tuple pointed to by p. If pos is out of bounds, returns NULL and sets an IndexError exception. Note: this function returns a ``borrowed'' reference.

PyObject* PyTuple_GET_ITEM (PyTupleObject *p, int pos)
Does the same, but does no checking of its arguments.

PyObject* PyTuple_GetSlice (PyTupleObject *p, int low, int high)
Takes a slice of the tuple pointed to by p from low to high and returns it as a new tuple.

int PyTuple_SetItem (PyTupleObject *p, int pos, PyObject *o)
Inserts a reference to object o at position pos of the tuple pointed to by p. It returns 0 on success.

void PyTuple_SET_ITEM (PyTupleObject *p, int pos, PyObject *o)

Does the same, but does no error checking, and should only be used to fill in brand new tuples.

int _PyTuple_Resize (PyTupleObject *p, int new, int last_is_sticky)
Can be used to resize a tuple. Because tuples are supposed to be immutable, this should only be used if there is only one module referencing the object. Do not use this if the tuple may already be known to some other part of the code. last_is_sticky is a flag -- if set, the tuple will grow or shrink at the front, otherwise it will grow or shrink at the end. Think of this as destroying the old tuple and creating a new one, only more efficiently.


Send comments on this document to python-docs@python.org.