3 ms·
For 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 t
by KayEss 10y ago
For 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.