PySequence_Length
を使用して、CでPython辞書の長さを取得しようとしています。PyDict_Size
を使用することができますが、もっと汎用的な関数特定の状況ではPython C-API:辞書で `PySequence_Length`を使用する
PyObject* d = PyDict_New();
Py_ssize_t res = PySequence_Length(d);
printf("Result : %ld\n", res);
if (res == -1) PyErr_Print();
これは失敗し、エラーを出力します。
TypeError: object of type 'dict' has no len()
私の質問は:なぜこれができませんの?
Py_ssize_t PySequence_Length(PyObject *o)
Returns the number of objects in sequence o on success, and -1 on failure. For objects that do not provide sequence protocol, this is equivalent to the Python expression len(o).
辞書タイプは__len__
属性を持たないため、及び(d
は辞書です)表現len(d)
ので、きちんと返す:Python辞書オブジェクトはシーケンスのプロトコルをサポートしていませんが、PySequence_Length
ためdocumentation氏は述べていますPythonの長さでは、なぜPySequence_Length
がCで失敗するのか分かりません。
何か説明がありますか?ここに書類が間違っていますか?