辞書オブジェクト (dictionary object)¶
- 
PyTypeObject PyDict_Type¶
- この - PyTypeObjectのインスタンスは Python の辞書を表現します。このオブジェクトは、Python レイヤにおける- dictと同じオブジェクトです。
- 
PyObject* PyDictProxy_New(PyObject *mapping)¶
- Return value: New reference.あるマップ型オブジェクトに対して、読み出し専用に制限された types.MappingProxyTypeオブジェクトを返します。通常、この関数は動的でないクラス型 (non-dynamic class type) のクラス辞書が変更されないようにビューを作成するために使われます。
- 
int PyDict_Contains(PyObject *p, PyObject *key)¶
- 辞書 p に key が入っているか判定します。p の要素が key に一致した場合は - 1を返し、それ以外の場合には- 0を返します。エラーの場合- -1を返します。この関数は Python の式- key in pと等価です。
- 
int PyDict_SetItem(PyObject *p, PyObject *key, PyObject *val)¶
- 辞書 p に、 key をキーとして値 val を挿入します。 key はハッシュ可能(hashable)でなければなりません。ハッシュ可能でない場合、 - TypeErrorを送出します。 成功した場合には- 0を、失敗した場合には- -1を返します。 この関数は val への参照を盗み取り ません。
- 
int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)¶
- Insert val 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- 0on success or- -1on failure. This function does not steal a reference to val.
- 
int PyDict_DelItem(PyObject *p, PyObject *key)¶
- Remove the entry in dictionary p with key key. key must be hashable; if it isn't, - TypeErroris raised. If key is not in the dictionary,- KeyErroris raised. Return- 0on success or- -1on failure.
- 
int PyDict_DelItemString(PyObject *p, const char *key)¶
- 辞書 p から文字列 key をキーとするエントリを除去します。key が辞書になければ、 - KeyErrorを送出します。成功した場合には- 0を、失敗した場合には- -1を返します。
- 
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 aconst char*, rather than aPyObject*.__hash__()メソッドや__eq__()メソッドの呼び出し中や、一時的な文字列オブジェクトの作成中に起こる例外は抑制されることに注意してください。 エラーを報告させるには、代わりにPyDict_GetItemWithError()を使ってください。
- 
PyObject* PyDict_SetDefault(PyObject *p, PyObject *key, PyObject *defaultobj)¶
- Return value: Borrowed reference.これは Python レベルの dict.setdefault()と同じです。 もしあれば、辞書 p から key に対応する値を返します。 キーが辞書になければ、値 defaultobj を挿入し defaultobj を返します。 この関数は、 key のハッシュ値を検索と挿入ごとに別々に評価するのではなく、一度だけしか評価しません。バージョン 3.4 で追加. 
- 
PyObject* PyDict_Items(PyObject *p)¶
- Return value: New reference.辞書内の全ての要素対が入った PyListObjectを返します。
- 
PyObject* PyDict_Keys(PyObject *p)¶
- Return value: New reference.辞書内の全てのキーが入った PyListObjectを返します。
- 
PyObject* PyDict_Values(PyObject *p)¶
- Return value: New reference.辞書 p 内の全ての値が入った PyListObjectを返します。
- 
Py_ssize_t PyDict_Size(PyObject *p)¶
- 辞書内の要素の数を返します。辞書に対して - len(p)を実行するのと同じです。
- 
int PyDict_Next(PyObject *p, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)¶
- Iterate over all key-value pairs in the dictionary p. The - Py_ssize_treferred to by ppos must be initialized to- 0prior to the first call to this function to start the iteration; the function returns true for each pair in the dictionary, and false once all pairs have been reported. The parameters pkey and pvalue should either point to- PyObject*variables that will be filled in with each key and value, respectively, or may be- NULL. Any references returned through them are borrowed. ppos should not be altered during iteration. Its value represents offsets within the internal dictionary structure, and since the structure is sparse, the offsets are not consecutive.- 例えば: - PyObject *key, *value; Py_ssize_t pos = 0; while (PyDict_Next(self->dict, &pos, &key, &value)) { /* do something interesting with the values... */ ... } - 反復処理中に辞書 p を変更してはなりません。辞書を反復処理する際に、キーに対応する値を変更しても大丈夫になりましたが、キーの集合を変更しないことが前提です。以下に例を示します: - 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)¶
- マップ型オブジェクト b の全ての要素にわたって、反復的にキー/値のペアを辞書 a に追加します。 b は辞書か、 - PyMapping_Keys()または- PyObject_GetItem()をサポートする何らかのオブジェクトにできます。 override が真ならば、 a のキーと一致するキーが b にある際に、既存のペアを置き換えます。それ以外の場合は、 b のキーに一致するキーが a にないときのみ追加を行います。成功した場合には- 0を返し、例外が送出された場合には- -1を返します。
- 
int PyDict_Update(PyObject *a, PyObject *b)¶
- C で表せば - PyDict_Merge(a, b, 1)と同じで、また Python の- a.update(b)と似ていますが、- PyDict_Update()は第二引数が "keys" 属性を持たない場合にキー/値ペアのシーケンスを反復することはありません。成功した場合には- 0を返し、例外が送出された場合には- -1を返します。
- 
int PyDict_MergeFromSeq2(PyObject *a, PyObject *seq2, int override)¶
- seq2 内のキー/値ペアを使って、辞書 a の内容を更新したり統合したりします。seq2 は、キー/値のペアとみなせる長さ 2 の反復可能オブジェクト(iterable object) を生成する反復可能オブジェクトでなければなりません。重複するキーが存在する場合、override が真ならば先に出現したキーを使い、そうでない場合は後に出現したキーを使います。成功した場合には - 0を返し、例外が送出された場合には- -1を返します。(戻り値以外は) 等価な Python コードを書くと、以下のようになります:- def PyDict_MergeFromSeq2(a, seq2, override): for key, value in seq2: if override or key not in a: a[key] = value