/Doc/c-api/dict.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 233 lines · 141 code · 92 blank · 0 comment · 0 complexity · 01de68970d5b8c15f8a53c7159a7f950 MD5 · raw file

  1. .. highlightlang:: c
  2. .. _dictobjects:
  3. Dictionary Objects
  4. ------------------
  5. .. index:: object: dictionary
  6. .. ctype:: PyDictObject
  7. This subtype of :ctype:`PyObject` represents a Python dictionary object.
  8. .. cvar:: PyTypeObject PyDict_Type
  9. .. index::
  10. single: DictType (in module types)
  11. single: DictionaryType (in module types)
  12. This instance of :ctype:`PyTypeObject` represents the Python dictionary
  13. type. This is exposed to Python programs as ``dict`` and
  14. ``types.DictType``.
  15. .. cfunction:: int PyDict_Check(PyObject *p)
  16. Return true if *p* is a dict object or an instance of a subtype of the dict
  17. type.
  18. .. versionchanged:: 2.2
  19. Allowed subtypes to be accepted.
  20. .. cfunction:: int PyDict_CheckExact(PyObject *p)
  21. Return true if *p* is a dict object, but not an instance of a subtype of
  22. the dict type.
  23. .. versionadded:: 2.4
  24. .. cfunction:: PyObject* PyDict_New()
  25. Return a new empty dictionary, or *NULL* on failure.
  26. .. cfunction:: PyObject* PyDictProxy_New(PyObject *dict)
  27. Return a proxy object for a mapping which enforces read-only behavior.
  28. This is normally used to create a proxy to prevent modification of the
  29. dictionary for non-dynamic class types.
  30. .. versionadded:: 2.2
  31. .. cfunction:: void PyDict_Clear(PyObject *p)
  32. Empty an existing dictionary of all key-value pairs.
  33. .. cfunction:: int PyDict_Contains(PyObject *p, PyObject *key)
  34. Determine if dictionary *p* contains *key*. If an item in *p* is matches
  35. *key*, return ``1``, otherwise return ``0``. On error, return ``-1``.
  36. This is equivalent to the Python expression ``key in p``.
  37. .. versionadded:: 2.4
  38. .. cfunction:: PyObject* PyDict_Copy(PyObject *p)
  39. Return a new dictionary that contains the same key-value pairs as *p*.
  40. .. versionadded:: 1.6
  41. .. cfunction:: int PyDict_SetItem(PyObject *p, PyObject *key, PyObject *val)
  42. Insert *value* into the dictionary *p* with a key of *key*. *key* must be
  43. :term:`hashable`; if it isn't, :exc:`TypeError` will be raised. Return
  44. ``0`` on success or ``-1`` on failure.
  45. .. cfunction:: int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)
  46. .. index:: single: PyString_FromString()
  47. Insert *value* into the dictionary *p* using *key* as a key. *key* should
  48. be a :ctype:`char\*`. The key object is created using
  49. ``PyString_FromString(key)``. Return ``0`` on success or ``-1`` on
  50. failure.
  51. .. cfunction:: int PyDict_DelItem(PyObject *p, PyObject *key)
  52. Remove the entry in dictionary *p* with key *key*. *key* must be hashable;
  53. if it isn't, :exc:`TypeError` is raised. Return ``0`` on success or ``-1``
  54. on failure.
  55. .. cfunction:: int PyDict_DelItemString(PyObject *p, char *key)
  56. Remove the entry in dictionary *p* which has a key specified by the string
  57. *key*. Return ``0`` on success or ``-1`` on failure.
  58. .. cfunction:: PyObject* PyDict_GetItem(PyObject *p, PyObject *key)
  59. Return the object from dictionary *p* which has a key *key*. Return *NULL*
  60. if the key *key* is not present, but *without* setting an exception.
  61. .. cfunction:: PyObject* PyDict_GetItemString(PyObject *p, const char *key)
  62. This is the same as :cfunc:`PyDict_GetItem`, but *key* is specified as a
  63. :ctype:`char\*`, rather than a :ctype:`PyObject\*`.
  64. .. cfunction:: PyObject* PyDict_Items(PyObject *p)
  65. Return a :ctype:`PyListObject` containing all the items from the
  66. dictionary, as in the dictionary method :meth:`dict.items`.
  67. .. cfunction:: PyObject* PyDict_Keys(PyObject *p)
  68. Return a :ctype:`PyListObject` containing all the keys from the dictionary,
  69. as in the dictionary method :meth:`dict.keys`.
  70. .. cfunction:: PyObject* PyDict_Values(PyObject *p)
  71. Return a :ctype:`PyListObject` containing all the values from the
  72. dictionary *p*, as in the dictionary method :meth:`dict.values`.
  73. .. cfunction:: Py_ssize_t PyDict_Size(PyObject *p)
  74. .. index:: builtin: len
  75. Return the number of items in the dictionary. This is equivalent to
  76. ``len(p)`` on a dictionary.
  77. .. versionchanged:: 2.5
  78. This function returned an :ctype:`int` type. This might require changes
  79. in your code for properly supporting 64-bit systems.
  80. .. cfunction:: int PyDict_Next(PyObject *p, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
  81. Iterate over all key-value pairs in the dictionary *p*. The
  82. :ctype:`Py_ssize_t` referred to by *ppos* must be initialized to ``0``
  83. prior to the first call to this function to start the iteration; the
  84. function returns true for each pair in the dictionary, and false once all
  85. pairs have been reported. The parameters *pkey* and *pvalue* should either
  86. point to :ctype:`PyObject\*` variables that will be filled in with each key
  87. and value, respectively, or may be *NULL*. Any references returned through
  88. them are borrowed. *ppos* should not be altered during iteration. Its
  89. value represents offsets within the internal dictionary structure, and
  90. since the structure is sparse, the offsets are not consecutive.
  91. For example::
  92. PyObject *key, *value;
  93. Py_ssize_t pos = 0;
  94. while (PyDict_Next(self->dict, &pos, &key, &value)) {
  95. /* do something interesting with the values... */
  96. ...
  97. }
  98. The dictionary *p* should not be mutated during iteration. It is safe
  99. (since Python 2.1) to modify the values of the keys as you iterate over the
  100. dictionary, but only so long as the set of keys does not change. For
  101. example::
  102. PyObject *key, *value;
  103. Py_ssize_t pos = 0;
  104. while (PyDict_Next(self->dict, &pos, &key, &value)) {
  105. int i = PyInt_AS_LONG(value) + 1;
  106. PyObject *o = PyInt_FromLong(i);
  107. if (o == NULL)
  108. return -1;
  109. if (PyDict_SetItem(self->dict, key, o) < 0) {
  110. Py_DECREF(o);
  111. return -1;
  112. }
  113. Py_DECREF(o);
  114. }
  115. .. versionchanged:: 2.5
  116. This function used an :ctype:`int *` type for *ppos*. This might require
  117. changes in your code for properly supporting 64-bit systems.
  118. .. cfunction:: int PyDict_Merge(PyObject *a, PyObject *b, int override)
  119. Iterate over mapping object *b* adding key-value pairs to dictionary *a*.
  120. *b* may be a dictionary, or any object supporting :func:`PyMapping_Keys`
  121. and :func:`PyObject_GetItem`. If *override* is true, existing pairs in *a*
  122. will be replaced if a matching key is found in *b*, otherwise pairs will
  123. only be added if there is not a matching key in *a*. Return ``0`` on
  124. success or ``-1`` if an exception was raised.
  125. .. versionadded:: 2.2
  126. .. cfunction:: int PyDict_Update(PyObject *a, PyObject *b)
  127. This is the same as ``PyDict_Merge(a, b, 1)`` in C, or ``a.update(b)`` in
  128. Python. Return ``0`` on success or ``-1`` if an exception was raised.
  129. .. versionadded:: 2.2
  130. .. cfunction:: int PyDict_MergeFromSeq2(PyObject *a, PyObject *seq2, int override)
  131. Update or merge into dictionary *a*, from the key-value pairs in *seq2*.
  132. *seq2* must be an iterable object producing iterable objects of length 2,
  133. viewed as key-value pairs. In case of duplicate keys, the last wins if
  134. *override* is true, else the first wins. Return ``0`` on success or ``-1``
  135. if an exception was raised. Equivalent Python (except for the return
  136. value)::
  137. def PyDict_MergeFromSeq2(a, seq2, override):
  138. for key, value in seq2:
  139. if override or key not in a:
  140. a[key] = value
  141. .. versionadded:: 2.2