Examples of polymorphism in Python

Consider the types of polymorphism without a reference to object-oriented programming, that is, in a broader sense.

Polymorphism of subtypes, or subtyping (subtyping).

Parametric polymorphism is used in generalized programming.

AD HOC Polymorphism, this is the overload of the methods based on the logic that the same method of the method can indicate different implementations depending on its parameters.

Let’s see how polymorphism is expressed. Examples will allow you to understand the principle of its action.

Example No. 1. There is an “+” operator, often used in programs that are created on Python. However, it can be used in different ways.

When it comes to integer types of data, “+” is used to add operands:

Number1 = 1

Number2 = 2

Print (Number1 + Number2)

As a result, the program will show the number 3.

In this case, “+” can also be used for the concatenation of the lines:

String1 = “Hello,”

string2 = “OTUS!”

Print (String1+””+String2)

We get the expected result “Hello, Otus!”.

This is one of the simplest examples of polymorphism on Python: we have only one “+” operator, which performs different operations for different types of data.

Example No. 2. Pieton has functions that allow you to take arguments of various types – for example, Len (). Let us consider in more detail how this happens.

Print (Len (“OOP programming”))

Print ([“Python”, “Java”, “C#”, “Scala”, “C ++”]))

print ({“Where to study?”: “In OTUS”, “How do classes go?”: “Online”}))

We get this result:




The fact is that the Len () function can interact with different types of data: line, list, motorcade, many, dictionary. In the above example, the same function returned specific information for each type of data, namely:

I calculated the number of letters in the word “programming”;

counted the number of words in the list;

I counted the number of keys in the dictionary.

Example No. 3. In Python, as in other programming languages, you can create classes that inherit methods and attributes of the base class. Some Methods and Attributes can be redefined in order to comply with the work class. This is called overwriting (Overriding). With the help of polymorphism, you can gain access to redundant methods and attributes with the same name as in the parental class.

Consider an example of redrawing this kind.

From Math Import Pi

Class Shape:

Def __init __ (Self, NAME):


Def Area (Self):


Def Fact (SELF):

Return “I am a two -dimensional figure”

Def __str __ (SELF):

Return Self.name

Class Square (Shape):

Def __init __ (Self, Length):

Super () .__ Init __ (“Square”)


Def Area (Self):


Def Fact (SELF):

Return “Any angle of square is 90 degrees.”

Class Circle (Shape):

Def __init __ (Self, Radius):

Super () .__ Init __ (“Circle”)

Self.Radius = Radius

Def Area (Self):

Return Pi*Self.Radius ** 2

A = square (5)

b = circle (8)

Print (b)

Print (B.Fact ())

Print (A.Fact ())

Print (b.area ())

We look at the output of the program:

A circle

I am a two -dimensional figure

Any square angle is 90 degrees.


This option of polymorphism works as follows. In this case, the __str __ () methods were used in the work of the code, they were not reduced in subsidiaries, that is, they are used directly from the parent class. The Pitonovsky interpreter automatically determined that the Fact () method for the A (Class Square) object is redeemed. As a result, the method that was determined in the classroom class is used.

The FACT () method for object B is not redeemed, so the method with the same name from the parent class (Shape) is used.

It is worth saying that Python does not support this option of redrawing the method as creating methods with the same name, but with various types of arguments.

Problems of Polymorphism of subtypes in OOP

Polymorphism of subtypes is one of the basic principles of object-oriented programming, many implementations of which are based on an incorrect idea of ​​it.

OOP has a huge number of disadvantages that negatively affect the entire industry as a whole. And novice programmers should know them. Unfortunately, object-oriented programming, despite its undeniable shortcomings, is still a base for quite promising programming languages.

Consider the basic principles of OOP.

Encapsulation. The independence of objects, within each of which are the necessary data.

Inheritance. The ability to use the same code so that one object can acquire the properties of another.

Polymorphism of subtypes. The property of the system due to which objects with the same interface can be used without information about the type and internal structure.

Capapsulation cannot be called unique for object-oriented programming. In many languages, it is implemented in different forms. These tools are one of the most useful in any library. Inheritance also has a fairly wide distribution without OOP, but is considered almost useless. Today, programmers gradually refuse to use it, while maintaining the normal functioning of the code.

The only unique principle of object-oriented programming is the polymorphism of subtypes. This is one of the forms of polymorphism, which many consider successful. There are several alternative solutions, for example, interfaces, lines polymorphism, parametric polymorphism and others. However, the polymorphism of subtypes is still used even in new languages.

In addition, in practice, subtypes have a strong connection with inheritance, despite the fact that they are usually mentioned separately from each other. For example, in order to create a subtype in from A, you need to inherit it from A, as presented in the picture.

It seems that this is a mistake, because there are subtypes or other forms of polymorphism that are not inherited. In addition, access to subtypes only through inheritance is mostly not too reliable.

Leave a Reply

Your email address will not be published. Required fields are marked *