15
atheist
7y

Duck typing

If something quacks that does not mean it's a duck. A duck has wings, can fly and is renowned for eating bread. For all you know what you have is a tape player.

Comments
  • 1
    Duck typing is an awful term. It's not that we conclude that it is a duck based on the behavior. It's that *we don't care*. It's the quackability that we care about, not the species.
  • 0
    My point is that one cannot infer behaviour. In python if something supports getitem with a number argument is it a dict or a list? Does it support slicing? Does everything passed to that function do those things?

    Sure, if you wrote the whole project you might know the answers but that's not how software dev works in the real world. You write code and hope it doesn't crash.

    That's a stupid fucking way of working.
  • 0
    There's always preconditions to functions that the type system cannot enforce.
  • 0
    True, but if you adhere to the object contract then you can be at least sure of code correctness to the api boundary. Then if someone else has done something stupid that's their problem.
  • 0
    Duck typing is similar, but you have more fine-grained control over the relevant parts of the contract, and you rely more on conventions in naming things.
Add Comment