Course
Dynamic Binding
Python Tutorial
This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.
Dynamic Binding
In object-oriented programming, the concept of dynamic binding is closely related to polymorphism. In Python, dynamic binding is the process of resolving a method or attribute at runtime, instead of at compile time.
According to the polymorphism feature, different objects respond differently to the same method call based on their individual implementations. This behavior is achieved through method overriding, where a subclass provides its own implementation of a method defined in its superclass.
The Python interpreter determines which is the appropriate method or attribute to invoke by based on the object's type or class hierarchy at runtime. This means that the specific method or attribute to be called is determined dynamically, based on the actual type of the object.
Example
The following example illustrates dynamic binding in Python
class shape: def draw(self): print ("draw method") return
class circle(shape): def draw(self): print ("Draw a circle") return
class rectangle(shape): def draw(self): print ("Draw a rectangle") return
shapes = [circle(), rectangle()]for shp in shapes: shp.draw()
It will produce the following output
Draw a circleDraw a rectangle
As you can see, the
draw()
method is bound dynamically to the corresponding implementation based on the object's type. This is how dynamic binding is implemented in Python.Duck Typing
Another concept closely related to dynamic binding is duck typing. Whether an object is suitable for a particular use is determined by the presence of certain methods or attributes, rather than its type. This allows for greater flexibility and code reuse in Python.
Duck typing is an important feature of dynamic typing languages like Python (Perl, Ruby, PHP, Javascript, etc.) that focuses on an object's behavior rather than its specific type. According to the "duck typing" concept, "If it walks like a duck and quacks like a duck, then it must be a duck."
Duck typing allows objects of different types to be used interchangeably as long as they have the required methods or attributes. The goal is to promote flexibility and code reuse. It is a broader concept that emphasizes on object behavior and interface rather than formal types.
Here is an example of duck typing
class circle: def draw(self): print ("Draw a circle") return
class rectangle: def draw(self): print ("Draw a rectangle") return
class area: def area(self): print ("calculate area") return
def duck_function(obj): obj.draw()
objects = [circle(), rectangle(), area()]for obj in objects: duck_function(obj)
It will produce the following output
Draw a circleDraw a rectangleTraceback (most recent call last): File "C:\Python311\hello.py", line 21, in <module> duck_function(obj) File "C:\Python311\hello.py", line 17, in duck_function obj.draw()AttributeError: 'area' object has no attribute 'draw'
The most important idea behind duck typing is that the
duck_function()
doesn't care about the specific types of objects it receives. It only requires the objects to have a draw()
method. If an object "quacks like a duck" by having the necessary behavior, it is treated as a "duck" for the purpose of invoking the draw()
method.Thus, in duck typing, the focus is on the object's behavior rather than its explicit type, allowing different types of objects to be used interchangeably as long as they exhibit the required behavior.