<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: 222010301033</title>
    <description>The latest articles on DEV Community by 222010301033 (@222010301033).</description>
    <link>https://dev.to/222010301033</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F675146%2Fd3f22a42-4ca3-4127-87de-416cb03113b6.png</url>
      <title>DEV Community: 222010301033</title>
      <link>https://dev.to/222010301033</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/222010301033"/>
    <language>en</language>
    <item>
      <title>POLYMORPHISM IN PYTHON

</title>
      <dc:creator>222010301033</dc:creator>
      <pubDate>Tue, 27 Jul 2021 16:27:43 +0000</pubDate>
      <link>https://dev.to/222010301033/polymorphism-in-python-4c70</link>
      <guid>https://dev.to/222010301033/polymorphism-in-python-4c70</guid>
      <description>&lt;p&gt;Polymorphism means multiple forms. In python we can find the same operator or function taking multiple forms. It also useful in creating different classes which will have class methods with same name. That helps in re using a lot of code and decreases code complexity. Polymorphism is also linked to inheritance as we will see in some examples below.&lt;/p&gt;

&lt;p&gt;Polymorphism in operators&lt;br&gt;
The + operator can take two inputs and give us the result depending on what the inputs are. In the below examples we can see how the integer inputs yield an integer and if one of the input is float then the result becomes a float. Also for strings, they simply get concatenated. This happens automatically because of the way the + operator is created in python.&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
a = 23&lt;br&gt;
b = 11&lt;br&gt;
c = 9.5&lt;br&gt;
s1 = "Hello"&lt;br&gt;
s2 = "There!"&lt;br&gt;
print(a + b)&lt;br&gt;
print(type(a + b))&lt;br&gt;
print(b + c)&lt;br&gt;
print(type (b + c))&lt;br&gt;
print(s1 + s2)&lt;br&gt;
print(type(s1 + s2))&lt;br&gt;
Running the above code gives us the following result −&lt;/p&gt;

&lt;p&gt;Output&lt;br&gt;
34&lt;br&gt;
20.5&lt;br&gt;
HelloThere!&lt;br&gt;
Polymorphism in in-built functions&lt;br&gt;
We can also see that different python functions can take inputs of different types and then process them differently. When we supply a string value to len() it counts every letter in it. But if we five tuple or a dictionary as an input, it processes them differently.&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
str = 'Hi There !'&lt;br&gt;
tup = ('Mon','Tue','wed','Thu','Fri')&lt;br&gt;
lst = ['Jan','Feb','Mar','Apr']&lt;br&gt;
dict = {'1D':'Line','2D':'Triangle','3D':'Sphere'}&lt;br&gt;
print(len(str))&lt;br&gt;
print(len(tup))&lt;br&gt;
print(len(lst))&lt;br&gt;
print(len(dict))&lt;br&gt;
Running the above code gives us the following result −&lt;/p&gt;

&lt;p&gt;Output&lt;br&gt;
10&lt;br&gt;
5&lt;br&gt;
4&lt;br&gt;
3&lt;br&gt;
Polymorphism in user-defined methods&lt;br&gt;
We can create methods with same name but wrapped under different class names. So we can keep calling the same method with different class name pre-fixed to get different result. In the below example we have two classes, rectangle and circle to get their perimeter and area using same methods.&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
from math import pi&lt;/p&gt;

&lt;p&gt;class Rectangle:&lt;br&gt;
   def &lt;strong&gt;init&lt;/strong&gt;(self, length, breadth):&lt;br&gt;
      self.l = length&lt;br&gt;
      self.b = breadth&lt;br&gt;
   def perimeter(self):&lt;br&gt;
      return 2*(self.l + self.b)&lt;br&gt;
   def area(self):&lt;br&gt;
      return self.l * self.b&lt;/p&gt;

&lt;p&gt;class Circle:&lt;br&gt;
   def &lt;strong&gt;init&lt;/strong&gt;(self, radius):&lt;br&gt;
      self.r = radius&lt;br&gt;
   def perimeter(self):&lt;br&gt;
      return 2 * pi * self.r&lt;br&gt;
   def area(self):&lt;br&gt;
      return pi * self.r ** 2&lt;/p&gt;

&lt;h1&gt;
  
  
  Initialize the classes
&lt;/h1&gt;

&lt;p&gt;rec = Rectangle(5,3)&lt;br&gt;
cr = Circle(4)&lt;br&gt;
print("Perimter of rectangel: ",rec.perimeter())&lt;br&gt;
print("Area of rectangel: ",rec.area())&lt;/p&gt;

&lt;p&gt;print("Perimter of Circle: ",cr.perimeter())&lt;br&gt;
print("Area of Circle: ",cr.area())&lt;br&gt;
Running the above code gives us the following result −&lt;/p&gt;

&lt;p&gt;Output&lt;br&gt;
Perimter of rectangel: 16&lt;br&gt;
Area of rectangel: 15&lt;br&gt;
Perimter of Circle: 25.132741228718345&lt;br&gt;
Area of Circle: 50.26548245743669&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
