Skip to main content

What is Duck Typing





I had originally answered this on Quora and thought that I should also make it available here in my blog for the readers who do not follow Quora (by the way it's a great site and you should follow that 😎).

Duck typing is a concept that says that the “type” of the object is a matter of concern only at runtime and you don’t need to to explicitly mention the type of the object before you perform any kind of operation on that object.

The following example can help in understanding this concept -
  1. def calc(a,b):
  2. return a+b
Now, Python says that for the above function I don’t need to be concerned about the “type” of the objects ‘a’ & ‘b’ and that the type will be taken care of during runtime as long as the objects support the ‘+’ . So, keeping this in mind the above function will work for any “type” of object which supports the operator + i.e. it will return valid values for a string, list or Integer. When I pass the following “types” of object then the function should work-
  1. calc(1,2) will return 1+2 = 3
  2. calc('hello','world') --> will return hello world
  3. calc([1],[2]) --> will return [1,2]
What we got in the above results?
  • In the first example of 1,2 since python interpreter recognizes these objects as type integer and since the operator + is valid for integers so the function calc returns a valid output of “type”integer 3. Here while passing the objects 1,2 to the function calc we are not defining that these objects are “type” integer as due to duck typing concept in python, the objects 1,2 are interpreted as integers and hence the function calc must return an output of type integer.
  • In the second example we did not explicitly mentioned anywhere that the objects hello & world are of “type” string, but duck typing concept comes into play and the interpreter recognizes these objects as strings during runtime and when these are passed to the function calc then the output should be a valid string ‘hello world’.
  • In the third example also duck typing can be noticed as the “type” of [1] & [2] is interpreted by python as lists and so the operator + should concatenate (which is a valid operation for lists) these two strings into a bigger string.
More about operators here

Now that you know what duck typing is and that it’s one of the powers of Python you can get started in learning the other features of Python and start exploring this programming language. If you found this post helpful and you have any suggestion to improve my blog then do post your comments below.

If you wanna get started in learning Python then why not get started with the python basics chapter. Check out this link to access the Python Basic Chapter for free.


If you want to download a copy of the chapter then purchase the chapter by clicking on the following link.

Additionally you can also bug me on Quora. You can also find me on Twitter or Facebook. So, why wait go ahead and start exploring.

Comments

Popular posts from this blog

What I learned from writing a guest post

                                                     Recently I wrote a python tutorial for Datacamp , while writing this tutorial I learned a couple of things which I am going to share with you right here in my blog post. May be you can use my experiences in your own endeavours as a writer. Idea The first step is idea generation. You can either search through any previous topics in your blog post if you have one or you can go through your past learning, experiences or any problem statement that you might have gone through and you will find that while doing that you can get tons of ideas to start your article. If you fall short of ideas then you can ask for help from may be your social media followings your friends or your family, for example I asked the people at Datacamp  themselves and they suggested me a couple of topics which got me st...

For Loop in python 3 : How to become a Python Jedi - Beginners Python Programming Tutorial - Part 7

I am back again with another part in the beginner python programming tutorial series. We will continue with "Loops" but this time we are going to learn how to use for loop in python 3.  Difference between For loop and While Loop A "For Loop" and "While Loop" have a common connection that both are looping statement, but major difference is that unlike while loop, a for loop gets executed a specified number of times. We can break down the For loop into the following components(Refer to the for loop flow chart below). for keyword variable name call to the range function colon operator clause or block of code Hovering over the flow-chart The above flow-chart is..well horrible I know, but I make my own drawings because I can't afford an artist. So, bear with me. In the above flowchart is a very basicy, laymanish representation of a for loop. The decision box contains the  For  statement along-with it's sou...

Expressions in Python : How to become a Python programming Jedi - A Python programming Tutorial Part 1

Long Long ago in a Galaxy far far away there was a planet named Quora. There was a growing agitation among the planet’s inhabitants to learn the way of the Python programming Force and become a master in the way of the Python programming Jedi, but very few knew that the secret art of the Jedi is embedded in a book named as TheCodingProject…. So, let’s begin the journey of learning the secret art of the Python programming Force and we shall together make the journey towards becoming the Python Jedi…. What is Python? As per Wikipedia “Python is a widely used high-level , general-purpose , interpreted , dynamic programming language .” Python is an open source programming language which was conceived in the late 1980s and its implementation began in December 1989 by Guido van Rossum (more on it here ). For us it’s sufficient to know that Python is : High level language - Python is a high-level programming language because it provides strong “:abstractio...