This kills me about python. So many times I cannot figure out what exactly a functions expects and what it returns, sometimes even from reading the documentation! Matplotlib is especially bad.
Re:Things I Was Wrong About: Types
by throwaway894345 on Sunday September 27, 2020 @01:15PM
(#24608203)
Good grief, matplotlib and the whole scientific computing stack are abysmal. Every function takes dozens of arguments, but any given call only has to pass some subset, and depending on the subset and their runtime types, the function just tries to guess what the caller wanted to do. I get that this stuff was written by amateurs, but in a saner world this stuff would have been addressed by now. Anyway, I moved on from Python to Go and I’m super happy (!!performance!!, !!package management!!, editor tooling, top notch document generation, quality ecosystem, !!static binaries!!, and types—even if Go’s type system isn’t as robust as Rust’s, it still goes a long way).
This is the most ridiculous thing about dynamic languages, I don't understand how people can be productive in such environment. Meanwhile I just press few keys and IDE shows my all about I need to know about that function. Oh, and it also runs 100x faster.
Re:Things I Was Wrong About: Types
by 6gvONxR4sf7o on Sunday September 27, 2020 @01:31PM
(#24608384)
kwargs makes me so angry in docs. Pass in anything! Learn the whole library to use this function!
I use a Python debugger in VSCode and hit the necessary breakpoints to figure it out.
Yes, it's bad, but it works. I hope there are better ways of doing it, but this is my way.
Didn't Python 3.5 introduce optional typings?
Only annotations which can be checked by mypy or other linters. I think cpython doesn't check them at runtime.
Re:Things I Was Wrong About: Types
by throwaway894345 on Sunday September 27, 2020 @01:18PM
(#24608241)
And mypy is still very immature. You can’t denote a recursive type (e.g., a JSON type) or specify a callback that takes keyword arguments. Even getting it to load type annotations from third party packages is hard in many cases. Worse, it seems to be improving at a snail’s pace if at all.
A "JSON" type in Python is just an untyped dictionary. Is that not what you're looking for?
Also, python's type-hinting supports forward-references which are what you would use for recursive or "self-referencing" types.
Personally, I like the slow pace they're taking with the typing. It's touching the core usage of the language in a fundamental way and I don't think that can be rushed. We're seeing lots of community growth around the type-hinting, even using them at run-time, which is amazing to watch and marvel at.
Re:Things I Was Wrong About: Types
by throwaway894345 on Sunday September 27, 2020 @11:45PM
(#24612764)
Untyped dictionaries are a superset of JSON. JSON is (more or less) precisely:
Dict[str, JSON] | List[JSON] | str | float | int | bool | None
Re:Things I Was Wrong About: Types
by ben-schaaf on Sunday September 27, 2020 @01:05PM
(#24608123)
Python indeed doesn't check them. Really they're just syntactic sugar for what you'd otherwise put in documentation.
Yes but hardly any code uses them yet. Hell there's still lots of code on Python 2 (yes really, no I'm not imagining it).
> the function signature told you nothing - like whether the function would even return or not would sometimes be a mystery
Yes! This drives me nuts about JavaScript! Untyped parameters are one thing, but having to read the entire function just to know if it returns anything is ridiculous.
Re:Things I Was Wrong About: Types
by eyelidlessness on Sunday September 27, 2020 @06:16PM
(#24610578)
To be fair, JS’s dynamic types aren’t solely to blame for this. There are dynamically typed languages that don’t require you to read an entire function body to know if it returns something. But, those are languages without statements, and all functions return something.
This was one of the pain-points when I was working more with node.js: the function signature told you nothing - like whether the function would even return or not would sometimes be a mystery.
In very, very short scripts you can get away without types (like in a notebook for example), but once a project starts to get even medium size the tiny amount of time you put into writing a type name explicitly here and there is more than made up for by the degree it helps with the structure and correctness of your program.