blob: 5be46c49f8fedcb24a5668c974ab3ce2504da7ec [file] [log] [blame]
Georg Brandlf6842722008-01-19 22:08:21 +00001.. highlightlang:: c
2
3.. _tupleobjects:
4
5Tuple Objects
6-------------
7
8.. index:: object: tuple
9
10
11.. ctype:: PyTupleObject
12
13 This subtype of :ctype:`PyObject` represents a Python tuple object.
14
15
16.. cvar:: PyTypeObject PyTuple_Type
17
18 .. index:: single: TupleType (in module types)
19
20 This instance of :ctype:`PyTypeObject` represents the Python tuple type; it is
21 the same object as ``tuple`` and ``types.TupleType`` in the Python layer..
22
23
24.. cfunction:: int PyTuple_Check(PyObject *p)
25
26 Return true if *p* is a tuple object or an instance of a subtype of the tuple
27 type.
28
29 .. versionchanged:: 2.2
30 Allowed subtypes to be accepted.
31
32
33.. cfunction:: int PyTuple_CheckExact(PyObject *p)
34
35 Return true if *p* is a tuple object, but not an instance of a subtype of the
36 tuple type.
37
38 .. versionadded:: 2.2
39
40
41.. cfunction:: PyObject* PyTuple_New(Py_ssize_t len)
42
43 Return a new tuple object of size *len*, or *NULL* on failure.
44
45
46.. cfunction:: PyObject* PyTuple_Pack(Py_ssize_t n, ...)
47
48 Return a new tuple object of size *n*, or *NULL* on failure. The tuple values
49 are initialized to the subsequent *n* C arguments pointing to Python objects.
50 ``PyTuple_Pack(2, a, b)`` is equivalent to ``Py_BuildValue("(OO)", a, b)``.
51
52 .. versionadded:: 2.4
53
54
55.. cfunction:: Py_ssize_t PyTuple_Size(PyObject *p)
56
57 Take a pointer to a tuple object, and return the size of that tuple.
58
59
60.. cfunction:: Py_ssize_t PyTuple_GET_SIZE(PyObject *p)
61
62 Return the size of the tuple *p*, which must be non-*NULL* and point to a tuple;
63 no error checking is performed.
64
65
66.. cfunction:: PyObject* PyTuple_GetItem(PyObject *p, Py_ssize_t pos)
67
68 Return the object at position *pos* in the tuple pointed to by *p*. If *pos* is
69 out of bounds, return *NULL* and sets an :exc:`IndexError` exception.
70
71
72.. cfunction:: PyObject* PyTuple_GET_ITEM(PyObject *p, Py_ssize_t pos)
73
74 Like :cfunc:`PyTuple_GetItem`, but does no checking of its arguments.
75
76
77.. cfunction:: PyObject* PyTuple_GetSlice(PyObject *p, Py_ssize_t low, Py_ssize_t high)
78
79 Take a slice of the tuple pointed to by *p* from *low* to *high* and return it
80 as a new tuple.
81
82
83.. cfunction:: int PyTuple_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)
84
85 Insert a reference to object *o* at position *pos* of the tuple pointed to by
86 *p*. Return ``0`` on success.
87
88 .. note::
89
90 This function "steals" a reference to *o*.
91
92
93.. cfunction:: void PyTuple_SET_ITEM(PyObject *p, Py_ssize_t pos, PyObject *o)
94
95 Like :cfunc:`PyTuple_SetItem`, but does no error checking, and should *only* be
96 used to fill in brand new tuples.
97
98 .. note::
99
100 This function "steals" a reference to *o*.
101
102
103.. cfunction:: int _PyTuple_Resize(PyObject **p, Py_ssize_t newsize)
104
105 Can be used to resize a tuple. *newsize* will be the new length of the tuple.
106 Because tuples are *supposed* to be immutable, this should only be used if there
107 is only one reference to the object. Do *not* use this if the tuple may already
108 be known to some other part of the code. The tuple will always grow or shrink
109 at the end. Think of this as destroying the old tuple and creating a new one,
110 only more efficiently. Returns ``0`` on success. Client code should never
111 assume that the resulting value of ``*p`` will be the same as before calling
112 this function. If the object referenced by ``*p`` is replaced, the original
113 ``*p`` is destroyed. On failure, returns ``-1`` and sets ``*p`` to *NULL*, and
114 raises :exc:`MemoryError` or :exc:`SystemError`.
115
116 .. versionchanged:: 2.2
117 Removed unused third parameter, *last_is_sticky*.
Christian Heimes3b718a72008-02-14 12:47:33 +0000118
119
Georg Brandl27cca3c2008-02-17 15:14:10 +0000120.. cfunction:: int PyTuple_ClearFreeList(void)
Christian Heimes3b718a72008-02-14 12:47:33 +0000121
122 Clear the free list. Return the total number of freed items.
123
124 .. versionadded:: 2.6