News for hackers. Stuff that matters.
Re:Functional Python Programming by carapace on Friday June 09, 2023 @04:54PM (#36264048)
> 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.

Re:Functional Python Programming by gnulinux on Friday June 09, 2023 @07:01PM (#36265490)
> 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.

Re:Functional Python Programming by carapace on Saturday June 10, 2023 @05:32PM (#36275357)
> 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.

Re:Functional Python Programming by linkdd on Saturday June 10, 2023 @06:27AM (#36269289)
> 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.