字典物件

PyDictObject

PyObject 子型態代表一個 Python 字典物件。

PyTypeObject PyDict_Type

PyTypeObject 實例代表一個 Python 字典型態。此與 Python 層中的 dict 為同一個物件。

int PyDict_Check(PyObject *p)

p 是一個字典物件或字典的子型態實例則會回傳 true。

int PyDict_CheckExact(PyObject *p)

p 是一個字典物件但並不是一個字典子型態的實例,則回傳 true。

PyObject* PyDict_New()
Return value: New reference.

返回一个新的空字典,失败时返回 NULL

PyObject* PyDictProxy_New(PyObject *mapping)
Return value: New reference.

Return a types.MappingProxyType object for a mapping which enforces read-only behavior. This is normally used to create a view to prevent modification of the dictionary for non-dynamic class types.

void PyDict_Clear(PyObject *p)

Empty an existing dictionary of all key-value pairs.

int PyDict_Contains(PyObject *p, PyObject *key)

Determine if dictionary p contains key. If an item in p is matches key, return 1, otherwise return 0. On error, return -1. This is equivalent to the Python expression key in p.

PyObject* PyDict_Copy(PyObject *p)
Return value: New reference.

Return a new dictionary that contains the same key-value pairs as p.

int PyDict_SetItem(PyObject *p, PyObject *key, PyObject *val)

Insert value into the dictionary p with a key of key. key must be hashable; if it isn't, TypeError will be raised. Return 0 on success or -1 on failure.

int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)

Insert value into the dictionary p using key as a key. key should be a const char*. The key object is created using PyUnicode_FromString(key). Return 0 on success or -1 on failure.

int PyDict_DelItem(PyObject *p, PyObject *key)

Remove the entry in dictionary p with key key. key must be hashable; if it isn't, TypeError is raised. Return 0 on success or -1 on failure.

int PyDict_DelItemString(PyObject *p, const char *key)

Remove the entry in dictionary p which has a key specified by the string key. Return 0 on success or -1 on failure.

PyObject* PyDict_GetItem(PyObject *p, PyObject *key)
Return value: Borrowed reference.

从字典 p 中返回以 key 为键的对象。 如果键名 key 不存在但 没有 设置一个异常则返回 NULL

需要注意的是,调用 __hash__()__eq__() 方法产生的异常不会被抛出。改用 PyDict_GetItemWithError() 获得错误报告。

PyObject* PyDict_GetItemWithError(PyObject *p, PyObject *key)
Return value: Borrowed reference.

PyDict_GetItem() 的变种,它不会屏蔽异常。 当异常发生时将返回 NULL 并且 设置一个异常。 如果键不存在则返回 NULL 并且不会 设置一个异常。

PyObject* PyDict_GetItemString(PyObject *p, const char *key)
Return value: Borrowed reference.

This is the same as PyDict_GetItem(), but key is specified as a const char*, rather than a PyObject*.

需要注意的是,调用 __hash__()__eq__() 方法和创建一个临时的字符串对象时产生的异常不会被抛出。改用 PyDict_GetItemWithError() 获得错误报告。

PyObject* PyDict_SetDefault(PyObject *p, PyObject *key, PyObject *defaultobj)
Return value: Borrowed reference.

This is the same as the Python-level dict.setdefault(). If present, it returns the value corresponding to key from the dictionary p. If the key is not in the dict, it is inserted with value defaultobj and defaultobj is returned. This function evaluates the hash function of key only once, instead of evaluating it independently for the lookup and the insertion.

3.4 版新加入.

PyObject* PyDict_Items(PyObject *p)
Return value: New reference.

Return a PyListObject containing all the items from the dictionary.

PyObject* PyDict_Keys(PyObject *p)
Return value: New reference.

Return a PyListObject containing all the keys from the dictionary.

PyObject* PyDict_Values(PyObject *p)
Return value: New reference.

Return a PyListObject containing all the values from the dictionary p.

Py_ssize_t PyDict_Size(PyObject *p)

Return the number of items in the dictionary. This is equivalent to len(p) on a dictionary.

int PyDict_Next(PyObject *p, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)

迭代字典 p 中的所有键值对。 在第一次调用此函数开始迭代之前,由 ppos 所引用的 Py_ssize_t 必须初始化为 0;该函数将为字典中的每个键值对返回真值,一旦所有键值对报告完毕则返回假值。 形参 pkeypvalue 应当指向 PyObject* 变量,它们将分别使用每个键和值来填充,或者也可以为 NULL。 通过它们返回的任何引用都是暂借的。 ppos 在迭代期间不应被更改。 它的值表示内部字典结构中的偏移量,并且由于结构是稀疏的,因此偏移量并不连续。

For example:

PyObject *key, *value;
Py_ssize_t pos = 0;

while (PyDict_Next(self->dict, &pos, &key, &value)) {
    /* do something interesting with the values... */
    ...
}

The dictionary p should not be mutated during iteration. It is safe to modify the values of the keys as you iterate over the dictionary, but only so long as the set of keys does not change. For example:

PyObject *key, *value;
Py_ssize_t pos = 0;

while (PyDict_Next(self->dict, &pos, &key, &value)) {
    long i = PyLong_AsLong(value);
    if (i == -1 && PyErr_Occurred()) {
        return -1;
    }
    PyObject *o = PyLong_FromLong(i + 1);
    if (o == NULL)
        return -1;
    if (PyDict_SetItem(self->dict, key, o) < 0) {
        Py_DECREF(o);
        return -1;
    }
    Py_DECREF(o);
}
int PyDict_Merge(PyObject *a, PyObject *b, int override)

Iterate over mapping object b adding key-value pairs to dictionary a. b may be a dictionary, or any object supporting PyMapping_Keys() and PyObject_GetItem(). If override is true, existing pairs in a will be replaced if a matching key is found in b, otherwise pairs will only be added if there is not a matching key in a. Return 0 on success or -1 if an exception was raised.

int PyDict_Update(PyObject *a, PyObject *b)

This is the same as PyDict_Merge(a, b, 1) in C, and is similar to a.update(b) in Python except that PyDict_Update() doesn't fall back to the iterating over a sequence of key value pairs if the second argument has no "keys" attribute. Return 0 on success or -1 if an exception was raised.

int PyDict_MergeFromSeq2(PyObject *a, PyObject *seq2, int override)

Update or merge into dictionary a, from the key-value pairs in seq2. seq2 must be an iterable object producing iterable objects of length 2, viewed as key-value pairs. In case of duplicate keys, the last wins if override is true, else the first wins. Return 0 on success or -1 if an exception was raised. Equivalent Python (except for the return value):

def PyDict_MergeFromSeq2(a, seq2, override):
    for key, value in seq2:
        if override or key not in a:
            a[key] = value
int PyDict_ClearFreeList()

Clear the free list. Return the total number of freed items.

3.3 版新加入.