> there is no immutable dict type
Does the namedtuple not suffice? Apologies if I'm being dense.
- - - -
It's not an immutable dict type (for one thing, this has linear lookup, the BTree would be better) but it's fun:
https://stackoverflow.com/questions/13708701/how-to-implemen...
In Python:
from functools import partial
def empty_dict(key):
raise KeyError
def _dict_add(dictionary, key, value, lookup):
return value if key == lookup else dictionary(lookup)
def dict_add(d, key, value):
return partial(_dict_add, d, key, value)
d = empty_dict
d = dict_add(d, 'key0', 23)
d = dict_add(d, 'key1', 18)
And then...
>>> d('key0')
23
>>> d('key1')
18
>>> d('keyn')
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
d('keyn')
File "/usr/home/sforman/tmp_fn_dict.py", line 7, in _dict_add
return value if key == lookup else dictionary(lookup)
File "/usr/home/sforman/tmp_fn_dict.py", line 7, in _dict_add
return value if key == lookup else dictionary(lookup)
File "/usr/home/sforman/tmp_fn_dict.py", line 4, in empty_dict
raise KeyError
KeyError
- - - -
> objects built with pydantic and type checked with mypy
FWIW, after ~15 years of professional Python development, I'm not into types [in Python]. My attitude is that if you really need them you should switch to e.g. OCaml or something that does them right. I love Python but I wouldn't use it anymore for anything other than scripts and maybe prototyping.