Hacker News new | ask | show | jobs
by anaphor 4599 days ago
I think the problem with adding even gradual typing to Python is how to handle duck typing. A lot of existing Python code relies on implicit assumptions about the parameters of functions, e.g. "this is an iterable" or "this is a number or supports some numeric operations". I know there are some Python libraries that formalize interfaces like this, but I can't see a good way of doing it nicely in general and being able to calculate the types of everything (with or without explicit annotations). Someone can correct me if I'm wrong but I don't think Scala or Clojure code has this problem.
2 comments

There is something called "abstract base classes" which provide the sort of interface testing you're asking for. You can, in Python, see if a thing supports (or more precisely claims to support) item access, iteration, sequence behavior, etc.
I hadn't thought about the duck typing issue. What I have in my mind is more akin to contract programming. I'd love to take a look at a library that does this. I just hacked something together during my layover to give it a try.