blob: 187ac017a7862a605eab06c30d007e1f62d20f54 [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _object:
4
5Object Protocol
6===============
7
8
Brian Curtin49281072011-08-11 09:41:31 -05009.. c:var:: PyObject* Py_NotImplemented
10
11 The ``NotImplemented`` singleton, used to signal that an operation is
12 not implemented for the given type combination.
13
14
15.. c:macro:: Py_RETURN_NOTIMPLEMENTED
16
17 Properly handle returning :c:data:`Py_NotImplemented` from within a C
18 function (that is, increment the reference count of NotImplemented and
19 return it).
20
21
Georg Brandl60203b42010-10-06 10:11:56 +000022.. c:function:: int PyObject_Print(PyObject *o, FILE *fp, int flags)
Georg Brandl54a3faa2008-01-20 09:30:57 +000023
24 Print an object *o*, on file *fp*. Returns ``-1`` on error. The flags argument
25 is used to enable certain printing options. The only option currently supported
26 is :const:`Py_PRINT_RAW`; if given, the :func:`str` of the object is written
27 instead of the :func:`repr`.
28
29
Georg Brandl60203b42010-10-06 10:11:56 +000030.. c:function:: int PyObject_HasAttr(PyObject *o, PyObject *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000031
32 Returns ``1`` if *o* has the attribute *attr_name*, and ``0`` otherwise. This
33 is equivalent to the Python expression ``hasattr(o, attr_name)``. This function
34 always succeeds.
35
36
Georg Brandl60203b42010-10-06 10:11:56 +000037.. c:function:: int PyObject_HasAttrString(PyObject *o, const char *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000038
39 Returns ``1`` if *o* has the attribute *attr_name*, and ``0`` otherwise. This
40 is equivalent to the Python expression ``hasattr(o, attr_name)``. This function
41 always succeeds.
42
43
Georg Brandl60203b42010-10-06 10:11:56 +000044.. c:function:: PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000045
46 Retrieve an attribute named *attr_name* from object *o*. Returns the attribute
47 value on success, or *NULL* on failure. This is the equivalent of the Python
48 expression ``o.attr_name``.
49
50
Georg Brandl60203b42010-10-06 10:11:56 +000051.. c:function:: PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000052
53 Retrieve an attribute named *attr_name* from object *o*. Returns the attribute
54 value on success, or *NULL* on failure. This is the equivalent of the Python
55 expression ``o.attr_name``.
56
57
Georg Brandl60203b42010-10-06 10:11:56 +000058.. c:function:: PyObject* PyObject_GenericGetAttr(PyObject *o, PyObject *name)
Benjamin Petersond23f8222009-04-05 19:13:16 +000059
60 Generic attribute getter function that is meant to be put into a type
61 object's ``tp_getattro`` slot. It looks for a descriptor in the dictionary
62 of classes in the object's MRO as well as an attribute in the object's
Serhiy Storchaka0b68a2d2013-10-09 13:26:17 +030063 :attr:`~object.__dict__` (if present). As outlined in :ref:`descriptors`,
64 data descriptors take preference over instance attributes, while non-data
Benjamin Petersond23f8222009-04-05 19:13:16 +000065 descriptors don't. Otherwise, an :exc:`AttributeError` is raised.
66
67
Georg Brandl60203b42010-10-06 10:11:56 +000068.. c:function:: int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000069
70 Set the value of the attribute named *attr_name*, for object *o*, to the value
71 *v*. Returns ``-1`` on failure. This is the equivalent of the Python statement
72 ``o.attr_name = v``.
73
74
Georg Brandl60203b42010-10-06 10:11:56 +000075.. c:function:: int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000076
77 Set the value of the attribute named *attr_name*, for object *o*, to the value
78 *v*. Returns ``-1`` on failure. This is the equivalent of the Python statement
79 ``o.attr_name = v``.
80
81
Georg Brandl60203b42010-10-06 10:11:56 +000082.. c:function:: int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value)
Benjamin Petersond23f8222009-04-05 19:13:16 +000083
84 Generic attribute setter function that is meant to be put into a type
85 object's ``tp_setattro`` slot. It looks for a data descriptor in the
86 dictionary of classes in the object's MRO, and if found it takes preference
87 over setting the attribute in the instance dictionary. Otherwise, the
Serhiy Storchaka0b68a2d2013-10-09 13:26:17 +030088 attribute is set in the object's :attr:`~object.__dict__` (if present).
89 Otherwise, an :exc:`AttributeError` is raised and ``-1`` is returned.
Benjamin Petersond23f8222009-04-05 19:13:16 +000090
91
Georg Brandl60203b42010-10-06 10:11:56 +000092.. c:function:: int PyObject_DelAttr(PyObject *o, PyObject *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000093
94 Delete attribute named *attr_name*, for object *o*. Returns ``-1`` on failure.
95 This is the equivalent of the Python statement ``del o.attr_name``.
96
97
Georg Brandl60203b42010-10-06 10:11:56 +000098.. c:function:: int PyObject_DelAttrString(PyObject *o, const char *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000099
100 Delete attribute named *attr_name*, for object *o*. Returns ``-1`` on failure.
101 This is the equivalent of the Python statement ``del o.attr_name``.
102
103
Benjamin Peterson1c262a62014-10-05 21:20:36 -0400104.. c:function:: PyObject* PyObject_GenericGetDict(PyObject *o, void *context)
Benjamin Peterson8eb12692012-02-19 19:59:10 -0500105
106 A generic implementation for the getter of a ``__dict__`` descriptor. It
107 creates the dictionary if necessary.
108
Benjamin Peterson43844352012-02-20 08:48:25 -0500109 .. versionadded:: 3.3
110
Benjamin Peterson8eb12692012-02-19 19:59:10 -0500111
Benjamin Peterson1c262a62014-10-05 21:20:36 -0400112.. c:function:: int PyObject_GenericSetDict(PyObject *o, void *context)
Benjamin Peterson8eb12692012-02-19 19:59:10 -0500113
114 A generic implementation for the setter of a ``__dict__`` descriptor. This
115 implementation does not allow the dictionary to be deleted.
116
Benjamin Peterson43844352012-02-20 08:48:25 -0500117 .. versionadded:: 3.3
118
Benjamin Peterson8eb12692012-02-19 19:59:10 -0500119
Georg Brandl60203b42010-10-06 10:11:56 +0000120.. c:function:: PyObject* PyObject_RichCompare(PyObject *o1, PyObject *o2, int opid)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000121
122 Compare the values of *o1* and *o2* using the operation specified by *opid*,
123 which must be one of :const:`Py_LT`, :const:`Py_LE`, :const:`Py_EQ`,
124 :const:`Py_NE`, :const:`Py_GT`, or :const:`Py_GE`, corresponding to ``<``,
125 ``<=``, ``==``, ``!=``, ``>``, or ``>=`` respectively. This is the equivalent of
126 the Python expression ``o1 op o2``, where ``op`` is the operator corresponding
127 to *opid*. Returns the value of the comparison on success, or *NULL* on failure.
128
129
Georg Brandl60203b42010-10-06 10:11:56 +0000130.. c:function:: int PyObject_RichCompareBool(PyObject *o1, PyObject *o2, int opid)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000131
132 Compare the values of *o1* and *o2* using the operation specified by *opid*,
133 which must be one of :const:`Py_LT`, :const:`Py_LE`, :const:`Py_EQ`,
134 :const:`Py_NE`, :const:`Py_GT`, or :const:`Py_GE`, corresponding to ``<``,
135 ``<=``, ``==``, ``!=``, ``>``, or ``>=`` respectively. Returns ``-1`` on error,
136 ``0`` if the result is false, ``1`` otherwise. This is the equivalent of the
137 Python expression ``o1 op o2``, where ``op`` is the operator corresponding to
138 *opid*.
139
Eli Benderskyad30c422011-01-15 10:23:34 +0000140.. note::
141 If *o1* and *o2* are the same object, :c:func:`PyObject_RichCompareBool`
142 will always return ``1`` for :const:`Py_EQ` and ``0`` for :const:`Py_NE`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000143
Georg Brandl60203b42010-10-06 10:11:56 +0000144.. c:function:: PyObject* PyObject_Repr(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000145
146 .. index:: builtin: repr
147
148 Compute a string representation of object *o*. Returns the string
149 representation on success, *NULL* on failure. This is the equivalent of the
Georg Brandl559e5d72008-06-11 18:37:52 +0000150 Python expression ``repr(o)``. Called by the :func:`repr` built-in function.
151
Nick Coghlanc0bc0b42014-02-09 12:00:01 +1000152 .. versionchanged:: 3.4
153 This function now includes a debug assertion to help ensure that it
154 does not silently discard an active exception.
Georg Brandl559e5d72008-06-11 18:37:52 +0000155
Georg Brandl60203b42010-10-06 10:11:56 +0000156.. c:function:: PyObject* PyObject_ASCII(PyObject *o)
Georg Brandl559e5d72008-06-11 18:37:52 +0000157
158 .. index:: builtin: ascii
159
Georg Brandl60203b42010-10-06 10:11:56 +0000160 As :c:func:`PyObject_Repr`, compute a string representation of object *o*, but
Georg Brandl559e5d72008-06-11 18:37:52 +0000161 escape the non-ASCII characters in the string returned by
Georg Brandl60203b42010-10-06 10:11:56 +0000162 :c:func:`PyObject_Repr` with ``\x``, ``\u`` or ``\U`` escapes. This generates
163 a string similar to that returned by :c:func:`PyObject_Repr` in Python 2.
Georg Brandl559e5d72008-06-11 18:37:52 +0000164 Called by the :func:`ascii` built-in function.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000165
Chris Jerdonekbb4e9412012-11-28 01:38:40 -0800166 .. index:: string; PyObject_Str (C function)
167
Georg Brandl54a3faa2008-01-20 09:30:57 +0000168
Georg Brandl60203b42010-10-06 10:11:56 +0000169.. c:function:: PyObject* PyObject_Str(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000170
Georg Brandl54a3faa2008-01-20 09:30:57 +0000171 Compute a string representation of object *o*. Returns the string
172 representation on success, *NULL* on failure. This is the equivalent of the
173 Python expression ``str(o)``. Called by the :func:`str` built-in function
174 and, therefore, by the :func:`print` function.
175
Nick Coghlan3d7b3642014-02-09 10:57:34 +1000176 .. versionchanged:: 3.4
Nick Coghlanc0bc0b42014-02-09 12:00:01 +1000177 This function now includes a debug assertion to help ensure that it
178 does not silently discard an active exception.
Nick Coghlan3d7b3642014-02-09 10:57:34 +1000179
Georg Brandl60203b42010-10-06 10:11:56 +0000180.. c:function:: PyObject* PyObject_Bytes(PyObject *o)
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000181
182 .. index:: builtin: bytes
183
Alexandre Vassalottieb6f8de2009-12-31 03:56:09 +0000184 Compute a bytes representation of object *o*. *NULL* is returned on
185 failure and a bytes object on success. This is equivalent to the Python
186 expression ``bytes(o)``, when *o* is not an integer. Unlike ``bytes(o)``,
187 a TypeError is raised when *o* is an integer instead of a zero-initialized
188 bytes object.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000189
Georg Brandl54a3faa2008-01-20 09:30:57 +0000190
Georg Brandl60203b42010-10-06 10:11:56 +0000191.. c:function:: int PyObject_IsSubclass(PyObject *derived, PyObject *cls)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000192
Georg Brandlf6d6dc22014-10-06 14:38:53 +0200193 Return ``1`` if the class *derived* is identical to or derived from the class
194 *cls*, otherwise return ``0``. In case of an error, return ``-1``.
195
196 If *cls* is a tuple, the check will be done against every entry in *cls*.
197 The result will be ``1`` when at least one of the checks returns ``1``,
198 otherwise it will be ``0``.
199
200 If *cls* has a :meth:`~class.__subclasscheck__` method, it will be called to
201 determine the subclass status as described in :pep:`3119`. Otherwise,
202 *derived* is a subclass of *cls* if it is a direct or indirect subclass,
203 i.e. contained in ``cls.__mro__``.
204
205 Normally only class objects, i.e. instances of :class:`type` or a derived
206 class, are considered classes. However, objects can override this by haivng
207 a :attr:`__bases__` attribute (which must be a tuple of base classes).
208
209
210.. c:function:: int PyObject_IsInstance(PyObject *inst, PyObject *cls)
211
212 Return ``1`` if *inst* is an instance of the class *cls* or a subclass of
213 *cls*, or ``0`` if not. On error, returns ``-1`` and sets an exception.
214
215 If *cls* is a tuple, the check will be done against every entry in *cls*.
216 The result will be ``1`` when at least one of the checks returns ``1``,
217 otherwise it will be ``0``.
218
219 If *cls* has a :meth:`~class.__instancecheck__` method, it will be called to
220 determine the subclass status as described in :pep:`3119`. Otherwise, *inst*
221 is an instance of *cls* if its class is a subclass of *cls*.
222
223 An instance *inst* can override what is considered its class by having a
224 :attr:`__class__` attribute.
225
226 An object *cls* can override if it is considered a class, and what its base
227 classes are, by having a :attr:`__bases__` attribute (which must be a tuple
228 of base classes).
Georg Brandl54a3faa2008-01-20 09:30:57 +0000229
230
Georg Brandl60203b42010-10-06 10:11:56 +0000231.. c:function:: int PyCallable_Check(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000232
233 Determine if the object *o* is callable. Return ``1`` if the object is callable
234 and ``0`` otherwise. This function always succeeds.
235
236
Georg Brandl60203b42010-10-06 10:11:56 +0000237.. c:function:: PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000238
239 Call a callable Python object *callable_object*, with arguments given by the
240 tuple *args*, and named arguments given by the dictionary *kw*. If no named
241 arguments are needed, *kw* may be *NULL*. *args* must not be *NULL*, use an
242 empty tuple if no arguments are needed. Returns the result of the call on
243 success, or *NULL* on failure. This is the equivalent of the Python expression
244 ``callable_object(*args, **kw)``.
245
246
Georg Brandl60203b42010-10-06 10:11:56 +0000247.. c:function:: PyObject* PyObject_CallObject(PyObject *callable_object, PyObject *args)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000248
249 Call a callable Python object *callable_object*, with arguments given by the
250 tuple *args*. If no arguments are needed, then *args* may be *NULL*. Returns
251 the result of the call on success, or *NULL* on failure. This is the equivalent
252 of the Python expression ``callable_object(*args)``.
253
254
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +0300255.. c:function:: PyObject* PyObject_CallFunction(PyObject *callable, const char *format, ...)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000256
257 Call a callable Python object *callable*, with a variable number of C arguments.
Georg Brandl60203b42010-10-06 10:11:56 +0000258 The C arguments are described using a :c:func:`Py_BuildValue` style format
Georg Brandl54a3faa2008-01-20 09:30:57 +0000259 string. The format may be *NULL*, indicating that no arguments are provided.
260 Returns the result of the call on success, or *NULL* on failure. This is the
261 equivalent of the Python expression ``callable(*args)``. Note that if you only
Georg Brandl60203b42010-10-06 10:11:56 +0000262 pass :c:type:`PyObject \*` args, :c:func:`PyObject_CallFunctionObjArgs` is a
Georg Brandl54a3faa2008-01-20 09:30:57 +0000263 faster alternative.
264
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +0300265 .. versionchanged:: 3.4
266 The type of *format* was changed from ``char *``.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000267
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +0300268
269.. c:function:: PyObject* PyObject_CallMethod(PyObject *o, const char *method, const char *format, ...)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000270
271 Call the method named *method* of object *o* with a variable number of C
Georg Brandl60203b42010-10-06 10:11:56 +0000272 arguments. The C arguments are described by a :c:func:`Py_BuildValue` format
Georg Brandl54a3faa2008-01-20 09:30:57 +0000273 string that should produce a tuple. The format may be *NULL*, indicating that
274 no arguments are provided. Returns the result of the call on success, or *NULL*
275 on failure. This is the equivalent of the Python expression ``o.method(args)``.
Georg Brandl60203b42010-10-06 10:11:56 +0000276 Note that if you only pass :c:type:`PyObject \*` args,
277 :c:func:`PyObject_CallMethodObjArgs` is a faster alternative.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000278
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +0300279 .. versionchanged:: 3.4
280 The types of *method* and *format* were changed from ``char *``.
281
Georg Brandl54a3faa2008-01-20 09:30:57 +0000282
Georg Brandl60203b42010-10-06 10:11:56 +0000283.. c:function:: PyObject* PyObject_CallFunctionObjArgs(PyObject *callable, ..., NULL)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000284
285 Call a callable Python object *callable*, with a variable number of
Georg Brandl60203b42010-10-06 10:11:56 +0000286 :c:type:`PyObject\*` arguments. The arguments are provided as a variable number
Georg Brandl54a3faa2008-01-20 09:30:57 +0000287 of parameters followed by *NULL*. Returns the result of the call on success, or
288 *NULL* on failure.
289
290
Georg Brandl60203b42010-10-06 10:11:56 +0000291.. c:function:: PyObject* PyObject_CallMethodObjArgs(PyObject *o, PyObject *name, ..., NULL)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000292
293 Calls a method of the object *o*, where the name of the method is given as a
294 Python string object in *name*. It is called with a variable number of
Georg Brandl60203b42010-10-06 10:11:56 +0000295 :c:type:`PyObject\*` arguments. The arguments are provided as a variable number
Georg Brandl54a3faa2008-01-20 09:30:57 +0000296 of parameters followed by *NULL*. Returns the result of the call on success, or
297 *NULL* on failure.
298
299
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000300.. c:function:: Py_hash_t PyObject_Hash(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000301
302 .. index:: builtin: hash
303
304 Compute and return the hash value of an object *o*. On failure, return ``-1``.
305 This is the equivalent of the Python expression ``hash(o)``.
306
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000307 .. versionchanged:: 3.2
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000308 The return type is now Py_hash_t. This is a signed integer the same size
309 as Py_ssize_t.
310
311
312.. c:function:: Py_hash_t PyObject_HashNotImplemented(PyObject *o)
Nick Coghlan7a70a3a2008-08-18 13:18:16 +0000313
Benjamin Petersone5384b02008-10-04 22:00:42 +0000314 Set a :exc:`TypeError` indicating that ``type(o)`` is not hashable and return ``-1``.
Nick Coghlan7a70a3a2008-08-18 13:18:16 +0000315 This function receives special treatment when stored in a ``tp_hash`` slot,
Benjamin Petersonc4fe6f32008-08-19 18:57:56 +0000316 allowing a type to explicitly indicate to the interpreter that it is not
Nick Coghlan7a70a3a2008-08-18 13:18:16 +0000317 hashable.
318
Nick Coghlan7a70a3a2008-08-18 13:18:16 +0000319
Georg Brandl60203b42010-10-06 10:11:56 +0000320.. c:function:: int PyObject_IsTrue(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000321
322 Returns ``1`` if the object *o* is considered to be true, and ``0`` otherwise.
323 This is equivalent to the Python expression ``not not o``. On failure, return
324 ``-1``.
325
326
Georg Brandl60203b42010-10-06 10:11:56 +0000327.. c:function:: int PyObject_Not(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000328
329 Returns ``0`` if the object *o* is considered to be true, and ``1`` otherwise.
330 This is equivalent to the Python expression ``not o``. On failure, return
331 ``-1``.
332
333
Georg Brandl60203b42010-10-06 10:11:56 +0000334.. c:function:: PyObject* PyObject_Type(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000335
336 .. index:: builtin: type
337
338 When *o* is non-*NULL*, returns a type object corresponding to the object type
339 of object *o*. On failure, raises :exc:`SystemError` and returns *NULL*. This
340 is equivalent to the Python expression ``type(o)``. This function increments the
341 reference count of the return value. There's really no reason to use this
342 function instead of the common expression ``o->ob_type``, which returns a
Georg Brandl60203b42010-10-06 10:11:56 +0000343 pointer of type :c:type:`PyTypeObject\*`, except when the incremented reference
Georg Brandl54a3faa2008-01-20 09:30:57 +0000344 count is needed.
345
346
Georg Brandl60203b42010-10-06 10:11:56 +0000347.. c:function:: int PyObject_TypeCheck(PyObject *o, PyTypeObject *type)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000348
349 Return true if the object *o* is of type *type* or a subtype of *type*. Both
350 parameters must be non-*NULL*.
351
352
Georg Brandl60203b42010-10-06 10:11:56 +0000353.. c:function:: Py_ssize_t PyObject_Length(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000354 Py_ssize_t PyObject_Size(PyObject *o)
355
356 .. index:: builtin: len
357
358 Return the length of object *o*. If the object *o* provides either the sequence
359 and mapping protocols, the sequence length is returned. On error, ``-1`` is
360 returned. This is the equivalent to the Python expression ``len(o)``.
361
Georg Brandl54a3faa2008-01-20 09:30:57 +0000362
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200363.. c:function:: Py_ssize_t PyObject_LengthHint(PyObject *o, Py_ssize_t default)
364
Larry Hastings3732ed22014-03-15 21:13:56 -0700365 Return an estimated length for the object *o*. First try to return its
366 actual length, then an estimate using :meth:`~object.__length_hint__`, and
367 finally return the default value. On error return ``-1``. This is the
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200368 equivalent to the Python expression ``operator.length_hint(o, default)``.
369
Armin Ronacher74b38b12012-10-07 10:29:32 +0200370 .. versionadded:: 3.4
371
Georg Brandldf48b972014-03-24 09:06:18 +0100372
Georg Brandl60203b42010-10-06 10:11:56 +0000373.. c:function:: PyObject* PyObject_GetItem(PyObject *o, PyObject *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000374
375 Return element of *o* corresponding to the object *key* or *NULL* on failure.
376 This is the equivalent of the Python expression ``o[key]``.
377
378
Georg Brandl60203b42010-10-06 10:11:56 +0000379.. c:function:: int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000380
381 Map the object *key* to the value *v*. Returns ``-1`` on failure. This is the
382 equivalent of the Python statement ``o[key] = v``.
383
384
Georg Brandl60203b42010-10-06 10:11:56 +0000385.. c:function:: int PyObject_DelItem(PyObject *o, PyObject *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000386
387 Delete the mapping for *key* from *o*. Returns ``-1`` on failure. This is the
388 equivalent of the Python statement ``del o[key]``.
389
390
Georg Brandl60203b42010-10-06 10:11:56 +0000391.. c:function:: PyObject* PyObject_Dir(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000392
393 This is equivalent to the Python expression ``dir(o)``, returning a (possibly
394 empty) list of strings appropriate for the object argument, or *NULL* if there
395 was an error. If the argument is *NULL*, this is like the Python ``dir()``,
396 returning the names of the current locals; in this case, if no execution frame
Georg Brandl60203b42010-10-06 10:11:56 +0000397 is active then *NULL* is returned but :c:func:`PyErr_Occurred` will return false.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000398
399
Georg Brandl60203b42010-10-06 10:11:56 +0000400.. c:function:: PyObject* PyObject_GetIter(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000401
402 This is equivalent to the Python expression ``iter(o)``. It returns a new
403 iterator for the object argument, or the object itself if the object is already
404 an iterator. Raises :exc:`TypeError` and returns *NULL* if the object cannot be
405 iterated.