Question

Is there a story behind the name ''duck typing'', I've heard ''if it looks like a duck, and sounds like a duck, let's call it a duck'' (or something like that), but why a duck? Why not ``if it looks like a _ and sounds like a _, let's call it a _''.

It sounds like a Flying Circus sketch, but I don't remember it. Is there a story behind the name?

Was it helpful?

Solution

Duck typing is a phrase borrowed from the much older "walk like a duck". The latter has been around far longer than the programming idea it describes. See answerbag.com

OTHER TIPS

This is not really a programming question, but my guess: Ducks walk funny and they quack. This is amusing enough to turn it into a figure of speech. :-)

Quoting Wikipedia's article on Duck Typing:

The name of the concept refers to the duck test, attributed to James Whitcomb Riley, which may be phrased as follows: "When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck."

...and from the same page...

Alex Martelli made an early (2000) use of the term in a message to the comp.lang.python newsgroup. He also highlighted misunderstanding of the literal duck test, which may indicate that the term was already in use:

"In other words, don't check whether it IS-a duck: check whether it QUACKS-like-a duck, WALKS-like-a duck, etc, etc, depending on exactly what subset of duck-like behaviour you need to play your language-games with."

No, the analogy is that if a variable looks like an integer (for example), and acts as an integer, it must be an integer (type).

Duck typing means 'no explicit predeclaraction of type'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top