4 ms·
In a lot of contexts for Python, static types don't add anything. A great example is the Norvig spell checker: https://norvig.com/spell-correct.html https://no
by choppaface 7y ago
In a lot of contexts for Python, static types don't add anything. A great example is the Norvig spell checker: https://norvig.com/spell-correct.html https://norvig.com/spell-correct.html
Furthermore, the duck typing paradigm further warrants Python's position here. In the above example, the spell checker code can work with a variety of string types, or even lists, or even perhaps the Tensor-like things found in Tensorflow and Pytorch. Without duck typing, the author has to use generics and the user has to play a type game that can become obscene-- see Java.
So that's a dominant mindset that sets the context here; things no doubt look strange from a different perspective.
I agree it's odd that the interpreter doesn't enforce static type declarations by default. I've seen now two professional code bases where the authors used type hints everywhere and I found the code actually disobeyed the hints. (I guess nobody ran mypy regularly).
That's probably what the Python committee anticipated: a few people would use this feature correctly, and hopefully build polished linting solutions as well. So they gave them an affordance for type hints. But most people would not want this at all, so it's entirely opt-in until it's something polished.
- Too 7y agoSorry but why wouldn't type hints add anything to that example? Type hints are usually there to help the user of the function, not to help with internal implementation. Just add word: str to all the functions and it will make it impossible to accidentally call any function with an invalid type. Especially the known(words) function is easy to misuse by providing a single string instead of a Iterable[str]. Iterable is a generic btw and will support both sets, lists and other similar types without any obscene type game. mypy also supports static duck typing with Protocols.
- phailhaus 7y ago> Furthermore, the duck typing paradigm further warrants Python's position here. Going to have to disagree with you here. Have you ever used Typescript? You can have duck typing and a proper static type system at the same time without devolving into Java. Furthermore, you can write Typescript without using types at all! Simply have everything default to `any`, and it reduces back down to Javascript. Python unfortunately decided to go in a direction that is the worst of both worlds. You can add type hints, but they do nothing without a 3rd party tool (?!). This means I can enter a code base, see type hints, and still have no guarantee that the types are what they claim to be. Instead, we have to rely on company-specific build processes to give us that static guarantee. And if you're not lucky enough to work in a codebase that runs mypy before merging, you might as well throw those type hints out the window.
- choppaface 7y agoI’ve used Scala which offers a similar experience. You still fall into the type game for generics, for example when using the just the Visitor pattern. Dealing with JSON is also a pain in any language with static types. I don’t believe you can really have duck typing and static at the same time unless you throw out the static checks and mark things as “Any” (or the equivalent). The beauty of Pythonic duck typing is that interfaces need not have any type declaration and furthermore the user can use dicts with complex structure (and declaring that structure with type annotations wouldn’t add any value over examples and/or unit tests). What makes Scala and modern C++ great is they offer nice interfaces for expedient use of the static type system. But there’s still plenty of room for the Pythonic approach of no static types at all.
- phailhaus 7y ago> declaring that structure with type annotations wouldn’t add any value over examples and/or unit tests Types can be exported, which means that consumers of your library have an actual guarantee of what you are returning, rather than just hoping that the docs are up to date. Javascript is the ultimate duck-typed language given that everything is really just an object, and TypeScript is extremely effective at managing it. I can't see why Python would be any different.
- choppaface 7y agoright, a lot of python code either doesn't result in libraries (or even documentation), or library APIs often tend to focus on dicts or integral types. successful libraries like numpy and pandas focus on a very small set of well-defined objects and leverage duck typing for containers (and runtime casting for interop with native code). I'm not saying arguments in favor of static typing and annotations are invalid, but rather it's a mindset that can make much of python look very foreign. the norvig spellchecker is a key example of swath of python code where static types are essentially irrelevant.