Duck typing is using objects by possibility rather than some named “type,” it’s a concept used a lot in Python.
It was put very well with a couple of examples on the python-dev mailing list.
Instead of checking whether the object, ob
, is an instance of type “Duck,” you check to see whether it offers the quack and duckwalk attributes. If it walks like a duck and sounds like a duck…
You can see now, potentially, how a program could be reused in ways that weren’t anticipated. It’s the root of Python’s polymorphism: If a program is written to work on strings, it can operate on any object that offers the same kind of possibilities as a string. As long as the new object has some part of its abstraction surface that feels that same as a string. The thing is, the language should be built around this concept.
[Since this talk, I’ve learned about Monkey Typing, which takes this idea much further. It doesn’t let your object masquerade as a Duck object, as duck typing would do, but it lets you declare that your object is “like” a Duck, and it lets you include in your object the transformations to make your object methods change into Duck methods. I’m not sure about it—it implies a level of collaboration and knowledge of other objects that might preclude evolution. But I’m probably wrong: My gut instinct is still talking Newtonian, step 2, programming. It tells me that objects should be different, and you shouldn’t have to explicitly set up your object to behave like other objects. My gut is telling me that objects have to stand alone and be abstracted from one another. But Step 3 tells me that a small amount of relatedness between objects is a good thing, because it sets up a network – a space – around which transformations can occur. So, we’ll see.]
I also know that there are experiments happening in elsewhere, that use these ideas. It’s very exciting. But it’s also just taking our Step 2 languages and bolting things on top of them. The great shift will come when we go right back and say: Okay, what does a Step 3 language really look like?
Matt Webb, posted 2005-09-02 (talk on 2005-06-11)