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.


Popular posts from this blog

How To Install Tensorflow On Windows

Why I am writing this tutorial ?
Recently I have started learning machine learning specifically DeepLearning for one of my pet projects, and during this quest of mine I wanted to learn TensorFlow library which is developed by Google. Learning is one thing but first I needed to install the library, but there was this problem which I faced during the installation. I am using Anaconda as my package manager and trying to install the Tensorflow library on my work computer which is behind a proxy, installing directly from the anaconda cloud while behind a proxy always displayed a proxy error.

How did I resolve the issue? Well this is exactly what I am going to write down in the below tutorial. The Steps To Follow
Before we get all cosy with this short tutorial on Tensorflow installation, let me tell you that
this tutorial is on the assumption that you have already installed Anaconda on your machine and you know at least a few basic things or two about python.
If you want to learn basic pyt…

Collatz sequence python : Monday fun with Python

Hello everyone. Today is Monday..well not a big deal and I know everyone knows today is a Monday. Like you I'm not a big fan of Monday, although nowadays I'm waiting eagerly for this day of the week as I get to watch a Game of Thrones episode on this day and I'm sure most of you can reciprocate the same feeling. The only thing I hate the most is the long wait till Monday night till I return to home from office and get to watch that episode for which I wait for one whole week and here I am in office waiting for the day to get over soon.

The plan To kill the time I do one of the thing that I love the most. Yes, you guessed right? I code. So, I decided to fool around some python programming code to kill the boredom. Without much ado let's code :) I decided to device a python program that would help me to devise a "collatz sequence" In case you are thinking what a "collatz sequence" is then dive into this link here.

For more understanding on python check…
Encountering The Python
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).
What kind of programing language?To give a better idea let's look at the below mentioned summary-
High level language- Python is a high-level language because it provides strong “abstraction”.Interpreted - Python is an Interpreted language because it does not run on a compiler.Dynamically typed language - Python is dynamically typed because in python every variable is bound to an object.Why should I learn Python?Python is a very powerful programming language, secondly it's easier to learn and since it's interpreted language the code is easier to understand. Lastly there is a huge community supporting Python so if you get stuck anywhere lot's of help are a…