Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Most articles that I have read explain that Duck Typing is a convenient way to access/call something without examining the type first. They then go on to show me examples of a function being called on passed in instances, some of these instances have types that have implemented the function being called and some haven't.

Before I start, please note that I am not asking for anyone to repeat the definition of duck typing. I would like someone to explain how it is supposed to be implemented.

I think the idea is to infer the supported behavior of a passed in instance without examining its type. This is opposed to examining the type of the instance before calling a function or referencing an attribute on it.

I do not see how any checking is used at all in the duck typing examples I have seen, and so I presume that's the point, simply call the function, and if an exception is throw of type "not supported", then handle that in the code. So how does this fit into the duck typing definition ? The passed in instance isnt being examined at all, so how can we tell it even looks, smells, or even walks like a duck ?

One of the explanations stated not checking the type results in less code, when really a better argument would be checking the type is useless anyway as you cant guarantee a type has implemented anything.

Then I see this argument, that duck typing is a style of programming


[^]

I get duck typing is treating something that looks/sounds/walks like a duck as a duck, but I dont see how that fits into calling functions/accessing attributes on an instance with a completely unknown type - how are you supposed to tell it's a duck ie see if it can indeed walk, or even smell it ? Is it because you expect it to be ? Is the whole thing about not relying on any type to tell you something has been implemented (so not using isinstance), or is it about the fact that you should just expect that no instance would be passed to this function in the first place if it didnt support the function being called on it ? So pure inference ? And how are errors handled ? Is all this expected to be in try block ?

What I have tried:

I've many articles (including wikipedia) on Duck Typing in Python and I am a bit confused as to what it is exactly. I've also searched on here as well as other programming forums, and the answers I have encountered have confused me further, so I am asking if anyone on here understand how duck typing fits into python usage, I am not asking for anyone to repeat the definition.
Posted
Updated 20-Dec-20 1:54am
v3

1 solution

Duck typing - Wikipedia[^] has an example in Python. The take home message seems to be that you can check, at run time, whether an object implements the method or contains the property that you are interested in. But the real question is: what actual problem are you trying to solve?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900