4 ms·
A type checker for Python can say: "pass", "reject" or "I don't know". Statically typed languages (1) introduce limitations so that type checking is decidable
by junke 10y ago
A type checker for Python can say: "pass", "reject" or "I don't know".
Statically typed languages (1) introduce limitations so that type checking is decidable (no incertitude), or (2) or treat "don't know" as "reject", because the runtime has practically no knowledge of type. This is not the case in Python, so you don't have to check everything. That does not mean you shouldn't check anything.
So there are still benefits in Python to have type in order to "reject" code that is undoubtedly going to produce type errors at runtime (or maybe to optimize things). In case of uncertainty, you know the runtime is going to catch errors anyway (including type errors).
- KayEss 10y agoFor structurally typed languages you really want something like C++'s concepts so you can talk about `Iterable[str]` rather than forcing it to `List[str]`. Is this possible with Python's type annotations?
- junke 10y agoI don't know exactly for Python. I would expect mixin class to be used for this. Also, maybe Python will allow you to define union types: e.g. X is either a "foo", a "bar" or a "zot" (all of them duck types the same implicit interface).
- KayEss 10y agoIt's kind of hard because you really want to be able to use generator functions as well. Maybe that just can't be properly described Python. It would be shame to lose out on passing a generator because somebody has used `List[int]` inside a function that really takes an iterable.
- raverbashing 10y agoAnd that's why it's bad. Let's throw something useful (duck typing) out of the window to please the Java people I'm sincerely disgusted
- junke 10y ago> Let's throw something useful (duck typing) Not sure why you say this is throwing away duck typing, you just don't add a type hint and you continue working as usual. > I'm sincerely disgusted I can sympathize, most of the time when there is a talk about Lisp there is a guy who comes just to say "dynamic typing is bad", etc. Well, even though there are definitely static analyzers for Lisp (see https://news.ycombinator.com/item?id=12216701 https://news.ycombinator.com/item?id=12216701), the language is still the same and doesn't prevent anyone from using dynamic features. People who really want statically typed programs won't touch Python anyway so I don't think you have much to worry.
- Leynos 10y agoYup. There is a Generic type which uses duck typing and has an `Iterable` subclass for classes implementing __iter__() (https://docs.python.org/3/library/typing.html#typing.Generic https://docs.python.org/3/library/typing.html#typing.Generic). It has been pointed out though that `str` counts as an instance of Iterable[str], so there is a possibility for some amusing issues to arise from this.
- alanfranzoni 10y agoIf I am programming in Python, I want nothing wrong to happen because I passed the "wrong type". That's the same argument as "don't use isinstance". It's not Pythonic. If you want a dynamically typed language, you don't check types. Period. Type hinting would be ok for documentation only. But then, a comment is fine and introduces less clutter.
- junke 10y agoWell, maybe "reject" was too strong a word. The warnings that are emitted by SBCL (Lisp implementation) are produced when two types don't intersect at all, something like "len(3)". There is no warning when types overlap. And even if the warning is issued, the code is still compiled and can run.