3 ms·
Even that trivial `print` annotation breaks duck-typing; the print function can actually accept anything that responds to `__str__`. Similarly, is there any re
by eevee 12y ago
Even that trivial `print` annotation breaks duck-typing; the print function can actually accept anything that responds to `__str__`.
Similarly, is there any reason the example `add` function in the OP shouldn't be able to add strings, or lists?
I've seen a lot of these quick-runtime-type-checking libraries, but they always have the same problem as manual type-checking: they make the constraints much stricter than they need to be and prevent entire classes of useful behavior.
- acjohnson55 12y agoIn some languages, like Scala, you can use structural type annotations to handle this situation.
- eevee 12y agoPython has the abc module, but it would be a bit wordier than "arg=list". Maybe if the builtins were treated as the corresponding abcs.
- acjohnson55 12y agoI think `abc` would be overkill for this. My understanding is that you might use that to add behavior to a bunch of classes that aren't related hiearchically. But for type safety, all you need to do is check for the single magic method.
- eevee 12y agoYou can use it for that, but you can also use it to check for rough interfaces. (The classes are rigged to fool isinstance() via dark magic.) It's fairly common that you want more than one magic method, but don't care too much what the type is as long as it's sorta like a `list` — thus, MutableMapping.