Question

Say you are doing a calculator in a dynamic language (Python etc...) and you have an add method.

def Add(x, y)
    print x + y

Now if you were to pass in anything but a number that would be wrong, so you need some datatype checking.

Is Duck Typing about objects as opposed to parameters like the above example?

Could anyone explain further?

Edit

By objects I mean:

Person.Quack()
Duck.Quack()

With no care about what gets passed into methods.

Was it helpful?

Solution

Duck typing is about not caring what the objects you're working with are as long as they support the necessary operations. So if + is string concatenation then passing strings to Add would be fine. If dates support the + operation then passing dates would be fine as well.

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