Introduction
At first, we’re going to discuss the terms duck typing and strong typing and then we’re going to see the examples of each.
Duck typing
In normal typing, if we have to check if an object can be used for a particular purpose, the suitability is determined by the type of object. But in duck typing, the suitability is determined by the presence of certain attributes, not by the type of the object itself.
Duck typing is a concept of dynamic typing in programming. It means the type of the variable is determined in runtime only.
In dynamic typing, the same object can have a different type during the time of execution.
The name duck typing came from the phrase – ” if it walks like a duck and quacks like a duck, it is a duck”. It means that python doesn’t care about which type of object it is unless and until it has required methods defined in it, it is going to execute.
Strong typing
Strong typing has stricter rules at the time of compilation. Due to this, the types become mandatory for performing certain operations.
If you are not getting anything at all, don’t worry about it. I will try to explain the concept of duck typing and strong typing with an example.
Duck Typing and Strong Typing Examples
#duck class class Duck: def fly(self): print("Duck fly") #eagle class class Eagle: def fly(self): print("Eagle fly") #class dog class Dog: def sound(self): print("Dog bark - woof woof") #function that takes object as an argument def allAnimals(obj): obj.fly() for obj in Duck(), Eagle(), Dog(): obj.fly()
Output
Duck fly Eagle fly --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-5-a9a8730f62f8> in <module> 19 20 for obj in Duck(), Eagle(), Dog(): ---> 21 obj.fly() AttributeError: 'Dog' object has no attribute 'fly'
Description
Here we have created three classes Duck, Eagle, and Dog. The first two classes have the fly() method
in them. The class Dog does not have the fly() method in it.
There is a function with the name all animals() that takes an object as an argument. If we take a look at the output the fly() method has been called when the object of respective classes is passed to allAnimals() function. Since Dog class doesn’t have the fly() method in it, it produces an error.
Here, it is not important which type of object has been passed to the allAnimal() function but if the class has the fly() method in it, it will execute and give the output. You can pass an object of any class to allAnimal() function and if that class has the fly() method in it, the method will execute to give output.
Strong typing use case
We can check whether the object passed to the function has the method being invoked or not. For this, we can use hasattr() function.
#duck class class Duck: def fly(self): print("Duck fly") #eagle class class Eagle: def fly(self): print("Eagle fly") #class dog class Dog: def sound(self): print("Dog bark - woof woof") #function that takes object as an argument def allAnimals(obj): if hasattr(obj, 'fly'): obj.fly() else: print("class", obj.__class__.__name__, "don't have method called fly") e = Eagle() d = Dog() allAnimals(e) allAnimals(d)
Output
Eagle fly class Dog don't have method called fly
Description
Before using strong typing while passing the object of Dog to the allAnimals() function, we got an error. We can apply some strict rules to avoid that error. Here we use hasattr() function to check whether the object passed to the function has the method that is invoked or not. If we take a look a the output, we can see there is no error in it. We get rid of that error using the concept of strong typing.
Conclusion
Duck typing is used for checking whether the object can be used for a particular purpose or not. It doesn’t depend upon the type of object but depends on the presence of particular methods or properties. Strong typing has strict rules due to which types of objects become essential for performing certain operations.
Happy Learning 🙂