0

I'd like to write my own dict container like the built-in dict but with a different hash function and collision resolution strategy.

PyDictObject, as Include\cpython\dictobject.h defines it, doesn't require change, but I'd like to write a different version of _Py_dict_lookup compared to the code in Objects\dictobject.c. (I'm referring to files in the CPython repository.)

What's a (simple) way to be able to change/add new files so that after rebuilding the interpreter, I can have

a = dict('key'='value')
b = my_dict('key2'='value2')

run successfully such that my_dict has a different hash table implementation (written in C, like dict does)?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
johnsmith
  • 515
  • 5
  • 12
  • 1
    Are you asking about the process of compiling the CPython source code, or how to actually implement a different dictionary in C? – mkrieger1 Sep 14 '21 at 12:49
  • 1
    Compiling a separate extension should be easier than modifying and compiling the whole interpreter. – Michael Butscher Sep 14 '21 at 12:55
  • 1
    Note that the standard `dict` implementation doesn't define a hash function - instead, *each individual object type* (that is suitable for use as a dict key) defines its own `__hash__()` method. If you really want to change that, you've got a *lot* of work ahead of you... – jasonharper Sep 14 '21 at 13:10
  • Maybe this can answer your question about the hash function: https://stackoverflow.com/a/37104288/15826727 . Although I'm not sure if a wrapping object is sufficient in your case. – Gábor Pálovics Sep 14 '21 at 14:30
  • @mkrieger1 a different dictionary – johnsmith Sep 15 '21 at 11:32

0 Answers0