
Baby duck by Shayne Kaye
I am a fan of Python, the programming language. Unfortunately, I did not have the time or the opportunity to use it as much as I would have liked.
One of the concept I have become to liked is Duck Typing. A good visual representation would be this simple sentence from James Whitcomb Riley.
If it walks like a duck and quacks like a duck, I would call it a duck.
Python uses duck typing extensively. There is no type check for method arguments and return values. If the function you are calling is expecting something that walks and quacks, it will call those methods appropriately and it will work. No matter if it is a duck or a baby pretending to be a duck, as long as it have those expected methods, properties or members, it will go through.
Today, I have been reading some interviews with Guido van Rossum, the Python author. In Contracts in Python, he talks about the implicit contract between the method user and the method creator. He argues that strongly-typed languages like Java or C#, interfaces are often used as contracts but that they get in the way as much as they help.
After digesting the whole thing, I have to agree with him.
I am currently working on projects using Coldfusion, C#, Java and Javascript. Coldfusion supports duck typing with the “any” type and the onMissingMethod event handler. I have been pleased to use it recently for my unit tests.
I have to come realize how much finger typing, work and code goes into type handling with C# and Java. There is a cost for it that put some weight onto maintenance. The more code you have, the more complicated it becomes to maintain and that includes code for type handling. I am not saying that there is no advantage to using a strongly-typed language, but that there is an alternative that shows many promises.
Comments 7
is it possible you could display an example of duck typing with python…
Posted 10 Mar 2009 at 12:47 am ¶Hello Carla,
A good example of this would be the file like objects used in Python. In various modules, you can use file like objects for different input/ouput operations.
For instance, take the pickle module: http://docs.python.org/library/pickle.html . It provides object de/serialization. It requires a file object for loading or dumping object. If you were to use an old tape component where there is no built in mechanism to access it, you could create a Tapefile class simply by specifying the few methods required by pickle. It could look like this:
class Tapefile():
“A tape file”
def __init__(self,params):
#various initialization for tape access
pass
def read(len):
#use special methods to access tape
pass
def readline():
#use special methods to read a whole line on tape
pass
def write(value):
#use special methods to write on tape
pass
You could than pass a Tapefile object to the pickle module and it would simply work. The pickle module do not care if it has a file object or another object pretending to act like a file. As long as it has the right methods, it will work.
Posted 15 Mar 2009 at 3:42 pm ¶Hello Tamela,
I have a deep respect for artists and creators like you. I wrongly assumed that your picture was licensed under a creative commons license because someone else uploaded it with that license on flickr.
Posted 12 Mar 2011 at 10:38 am ¶To Remy
Posted 14 Mar 2011 at 11:35 pm ¶Oh OK. Will you please remove my comments with my address. Thank you. Sorry for the confusion. I appreciate that you took the image down. Thanks again.
Tamela
Slam dunkin like Shaquille O’Neal, if he wrote inforamtvie articles.
Posted 19 Jul 2011 at 12:14 pm ¶gCy3t5 xvxeirwehfnl
Posted 20 Jul 2011 at 8:26 am ¶mamfsn dgtepvqnqjnb
Posted 22 Jul 2011 at 6:57 am ¶Post a Comment