What is duck typing?

_config.yml

What is duck typing?

Shamelessly stolen from: (check it out it’s quite cool): https://s-media-cache-ak0.pinimg.com/564x/40/cf/37/40cf376037a22f614efb2374267a41e4.jpg

Ask a programmer what it is and you’ll get three different responses, but this is the general idea of what most people mean by it:

Duck Typing

(“If it walks like a duck and quacks like a duck then it is a duck.”) - yes but what does that mean? This is best illustrated by example:

Example:

Imagine I have a magically ability. Special powers. If I say “Drive!” to a car, well then, it drives!

Does it work on other things? Not sure: so I try it on a truck. Wow - it drives too! I then try it on planes, trains and 1 Woods. They all drive!

But would it work on say, a teacup? Error: KAAAA-BOOOOOOM! that didn’t work out so good. ====> Teacups can’t drive!! duh!?

This is basically the concept of duck typing. It’s a try-before-you-buy type system. If it works, all is well. But if it fails, well, it’s gonna blow up in your face.

In other words, we are interested in what the object can do, rather than with what the object is.

If we were concerned with what the object actually was, then our magic trick will work on cars, but will fail on other objects which can drive: trucks, mopeds, tuk-tuks etc. It won’t work on trucks because our magic wand is expecting it to only work on cars. The only way you can get a truck to drive is if you can somehow get the magic want to expect both trucks and cars (perhaps by “implementing a common interface”). If you don’t know what that means then don’t worry.

Summary

The important thing to take out of this is, is that what’s important in duck typing is what the object can actually do, rather than what the object is.

Hope that helps.

_config.yml

Written on December 9, 2016