4 ms·
> 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, thi
by carapace 3y ago
> 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-implement-a-dictionary-as-a-function-in-ocaml 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.
- gnulinux 3y ago> My attitude is that if you really need them you should switch to e.g. OCaml or something that does them right. I strongly disagree. I personally think there is no programming problem where types aren't useful. When I write code, I think in terms of types, and rarely in terms of anything else. Python is useful in tons of problems OCaml isn't such as data science, statistics etc. Besides python has a very large ecosystem which matters in order not to reinvent the wheel.
- carapace 3y ago> I personally think there is no programming problem where types aren't useful. I'd have to disagree with that just on first principles: no absolutes. (Note that that's an absolute.) :) Anyway, I'm not trying to argue that types are not useful, they are. What I'm saying is that Python's loose approach to typing is useful "in the small" so to speak, but as projects grow larger you want "harder" types, checked by machine. Same thing with DB schema: No-SQL only makes sense in the small domains where the schema can be ignored or treated as an afterthought. Above a certain size and you grow a schema again and might as well do it right.
- linkdd 3y ago> I'm not into types [in Python] In Python type hints are just that: hints. mypy is a very poor static analyzer, and a lot of Python code (especially in frameworks like Django) is inherently dynamic and cannot be type-checked. And that's ok. Though, documenting your API with type-hints really helps, especially with VSCode (via pyright) popovers when you hover a symbol/function.