PyHash API

See also the PyTypeObject.tp_hash member and 數值型別的雜湊.

type Py_hash_t

雜湊值型別:有符號整數。

在 3.2 版被加入.

type Py_uhash_t

雜湊值型別:無符號整數。

在 3.2 版被加入.

PyHASH_MODULUS

The Mersenne prime P = 2**n -1, used for numeric hash scheme.

在 3.13 版被加入.

PyHASH_BITS

The exponent n of P in PyHASH_MODULUS.

在 3.13 版被加入.

PyHASH_MULTIPLIER

Prime multiplier used in string and various other hashes.

在 3.13 版被加入.

PyHASH_INF

The hash value returned for a positive infinity.

在 3.13 版被加入.

PyHASH_IMAG

The multiplier used for the imaginary part of a complex number.

在 3.13 版被加入.

type PyHash_FuncDef

PyHash_GetFuncDef() 所使用的雜湊函式定義。

const char *name

雜湊函式名稱(UTF-8 編碼字串)。

const int hash_bits

雜湊值的內部大小(以位元為單位)。

const int seed_bits

Seed 輸入的大小(以位元為單位)。

在 3.4 版被加入.

PyHash_FuncDef *PyHash_GetFuncDef(void)

取得雜湊函式定義。

也參考

PEP 456「安全且可交替使用的雜湊演算法 (Secure and interchangeable hash algorithm)」。

在 3.4 版被加入.

Py_hash_t Py_HashPointer(const void *ptr)

Hash a pointer value: process the pointer value as an integer (cast it to uintptr_t internally). The pointer is not dereferenced.

The function cannot fail: it cannot return -1.

在 3.13 版被加入.

Py_hash_t Py_HashBuffer(const void *ptr, Py_ssize_t len)

Compute and return the hash value of a buffer of len bytes starting at address ptr. The hash is guaranteed to match that of bytes, memoryview, and other built-in objects that implement the buffer protocol.

Use this function to implement hashing for immutable objects whose tp_richcompare function compares to another object's buffer.

len must be greater than or equal to 0.

This function always succeeds.

在 3.14 版被加入.

Py_hash_t PyObject_GenericHash(PyObject *obj)

Generic hashing function that is meant to be put into a type object's tp_hash slot. Its result only depends on the object's identity.

CPython 實作細節: In CPython, it is equivalent to Py_HashPointer().

在 3.13 版被加入.