Back to Browse

Polymorphism in Python

258 views
Dec 5, 2024
12:53

#polymorphism #python #oop In this lecture, we are going to explore Polymorphism in Python. Polymorphism is a key concept in object-oriented programming, and Python handles it in an intuitive way. First, we'll introduce what polymorphism is and why it's important. Then, we'll explore its various forms, such as compile-time and runtime polymorphism, and how Python emulates method overloading. We'll also look at how polymorphism works with functions and classes, and how it plays a role in inheritance. Additionally, we'll cover abstract base classes and how they relate to polymorphism. Finally, we'll wrap up with a discussion on duck typing. Let's begin! Polymorphism comes from two Greek words: 'poly' meaning many, and 'morph' meaning forms. So, polymorphism refers to something that can take many forms. In Python, it refers to the ability of different objects to respond to the same method call in their own unique ways. This helps make our code more flexible and extendable. It's one of the four pillars of object-oriented programming alongside inheritance, encapsulation, and abstraction. By using polymorphism, we can write cleaner and more adaptable code, since objects of different classes can be treated as instances of the same class through a shared interface. There are two types of polymorphism we need to talk about: compile-time polymorphism, which includes method overloading, and runtime polymorphism, which involves method overriding. Method Overloading refers to defining multiple methods with the same name but different parameters. Python doesn't support this directly, but we can use default or variable-length arguments to achieve a similar effect. Method Overriding occurs when a child class provides a specific implementation of a method that is already defined in its parent class. First, let's talk about method overloading. In other languages like Java or C++, we can have multiple functions with the same name but different parameter lists. This is called method overloading and happens at compile time. In Python, though, we don't have method overloading in the traditional sense. However, we can emulate it using default arguments or variable-length arguments. Default arguments allow you to assign default values to parameters in a function. This means if you call the function without providing those arguments, the function will use the default values. Let's look at an example. I'll create a new folder for our 'polymorphism' practices, and then add a new file called 'poly_emulate.py'. We'll define a function called add, which takes three parameters. The first parameter, a, is required, while b and c have default values of 0. This means we can call the function with one, two, or three arguments. If we only provide one argument, the function will use the default values for b and c. The function then returns the sum of all three parameters. This approach allows us to handle different numbers of arguments with a single function, offering flexibility without needing to overload the function. When we run this script, We see that all the three forms work. This simulates overloading by using default arguments. We could also use variable-length arguments (*args) to mimic polymorphism. As we already learned, this feature allows a function to accept a varying number of arguments. Let's modify our function using *args. Inside the function, args is treated as a tuple containing all the arguments passed. Now, we can use our function for adding any number of arguments. If we run the code again, We should see any of the forms work. By using *args, the add function can handle any number of arguments, mimicking polymorphism without the need for explicitly defined parameters with default values. As a result, even though Python doesn't support compile-time polymorphism directly, we can mimic it by using features like default or variable-length arguments. Now, moving on to method overriding, which happens at runtime. This is when a method in a subclass has the same name as a method in its parent class, but the implementation in the subclass is different. To better understand method overriding, let's create a new example called 'poly_runtime.py'. In this example, we'll define two classes: a parent class, which we'll call Animal and has a method sound, And, a child class, Dog, that will inherit from Animal and overrides the sound method to return 'Bark' instead of the generic sound. Now, let's instantiate both and call their sound methods. When we run this code, We'll see that even though both classes have the same method name, the output depends on the object calling the method. This is polymorphism in action, with different behaviors for the same method name depending on the object type. Let's extend our example by adding two more subclasses. A subclass Cat, which overrides the sound method and returns 'Meow', which is specific to cats. And, a subclass Pig, which does not override the sound method, and stays with the...

Download

1 formats

Video Formats

360pmp416.2 MB

Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.

Polymorphism in Python | NatokHD