Polymorphism in Python

Polymorphism is derived from the Greek words poly (many) and morphism (morphology) (forms). It means that different kinds can use the same function name. This simplifies and simplifies programming. Polymorphism is defined as having several forms. Polymorphism in programming refers to the usage of the same function name for various kinds.

In Python, a child class inherits all of the parent class’s methods. However, in some cases, the parent class’s inherited function does not fully fit within the child class. We’ll have to re-implement the method in the child class in such circumstances.

For example, the ‘+’ operator has many uses. The + operator is used to execute arithmetic addition on integer data types. Concatenation of string data types is also performed using the + operator. Different operations for different data kinds have been carried out using a single operator +. In Python, this is one of the most basic examples of polymorphism.

Similarly, the len() method can work with a variety of data types. The len() method can deal with a variety of data types, including text, list, tuple, set, and dictionary, however it only delivers information about certain data types.

Because Python permits multiple classes to have methods with the same name, we can leverage the concept of polymorphism when constructing class methods. These methods can be called without consideration for the object we’re dealing with in the future.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store