Re:Type checking in Python
by acjohnson55 on Sunday May 18, 2014 @10:48PM
(#7765448)
In some languages, like Scala, you can use structural type annotations to handle this situation.
[ Parent
| on HN ]
Python has the abc module, but it would be a bit wordier than "arg=list". Maybe if the builtins were treated as the corresponding abcs.
Re:Type checking in Python
by acjohnson55 on Monday May 19, 2014 @08:05AM
(#7766995)
I 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.
You 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.