{"id":955,"date":"2019-12-15T07:36:58","date_gmt":"2019-12-15T07:36:58","guid":{"rendered":"https:\/\/www.askpython.com\/?p=955"},"modified":"2022-08-06T13:08:34","modified_gmt":"2022-08-06T13:08:34","slug":"polymorphism-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/oops\/polymorphism-in-python","title":{"rendered":"Polymorphism in Python"},"content":{"rendered":"\n<p>Polymorphism means having vivid or different forms. In the programming world, Polymorphism refers to the ability of the function with the same name to carry different functionality altogether. It creates a structure that can use many forms of objects. <\/p>\n\n\n\n<p>This permits <a href=\"https:\/\/www.askpython.com\/python\/python-functions\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"functions (opens in a new tab)\">functions<\/a>\/arguments to use entities of different types at different times.<\/p>\n\n\n\n<p>In object-oriented programming, Polymorphism allows a particular object referring to a particular class to be used in a similar fashion as if it was a different object referring to altogether a different class.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"703\" height=\"214\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Polymorphism-in-Python.png\" alt=\"Polymorphism In Python\" class=\"wp-image-1150\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Polymorphism-in-Python.png 703w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Polymorphism-in-Python-300x91.png 300w\" sizes=\"auto, (max-width: 703px) 100vw, 703px\" \/><figcaption>Polymorphism In Python<\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Implementing Polymorphism in Python with Class<\/h2>\n\n\n\n<p>Python can use different types of <a href=\"https:\/\/www.askpython.com\/python\/oops\/python-classes-objects\">classes<\/a>, in the same way, using Polymorphism. To serve this purpose, one can create a loop that iterates through a tuple of objects. Post which, one can call the methods without having a look at the type of class to which the object belongs to.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Polymorphism with Classes and Objects<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Rabbit(): \n    def age(self): \n        print(&quot;This function determines the age of Rabbit.&quot;) \n  \n    def color(self): \n        print(&quot;This function determines the color of Rabbit.&quot;) \n  \nclass Horse(): \n    def age(self): \n        print(&quot;This function determines the age of Horse.&quot;) \n  \n    def color(self): \n        print(&quot;This function determines the color of Horse.&quot;) \n  \nobj1 = Rabbit() \nobj2 = Horse() \nfor type in (obj1, obj2): # creating a loop to iterate through the obj1, obj2\n    type.age() \n    type.color() \n    \n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<p>This function determines the age of Rabbit.<br>This function determines the color of Rabbit.<br>This function determines the age of Horse.<br>This function determines the color of Horse. <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Implementing Polymorphism in Python with Inheritance<\/h2>\n\n\n\n<p>We will be defining functions in the derived class that has the same name as the functions in the base class. Here, we re-implement the functions in the derived class. The phenomenon of re-implementing a function in the derived class is known as <strong>Method Overriding<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Polymorphism with Inheritance<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Animal: \n  def type(self): \n    print(&quot;Various types of animals&quot;) \n      \n  def age(self): \n    print(&quot;Age of the animal.&quot;) \n    \nclass Rabbit(Animal): \n  def age(self): \n    print(&quot;Age of rabbit.&quot;) \n      \nclass Horse(Animal): \n  def age(self): \n    print(&quot;Age of horse.&quot;) \n      \nobj_animal = Animal() \nobj_rabbit = Rabbit() \nobj_horse = Horse() \n  \nobj_animal.type() \nobj_animal.age() \n  \nobj_rabbit.type() \nobj_rabbit.age() \n  \nobj_horse.type() \nobj_horse.age() \n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<p>Various types of animals                                                                                                                      <br> Age of the animal.                                                                                                                            <br> Various types of animals                                                                                                                      <br> Age of rabbit.                                                                                                                                <br> Various types of animals                                                                                                                      <br> Age of horse.  <\/p>\n\n\n\n<p><strong>Recommended Readings:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.askpython.com\/python\/oops\/inheritance-in-python\">Inheritance in Python<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/oops\/python-multiple-inheritance\">Multiple Inheritance in Python<\/a><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Compile-Time Polymorphism or Method Overloading?<\/h2>\n\n\n\n<p>Unlike many other popular object-oriented programming languages such as Java, Python doesn&#8217;t support compile-time polymorphism or method overloading. If a class or Python script has multiple methods with the same name, the method defined in the last will override the earlier one. <\/p>\n\n\n\n<p>Python doesn&#8217;t use function arguments for method signature, that&#8217;s why method overloading is not supported in Python.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Operator Overloading in Python<\/h2>\n\n\n\n<p>Python supports operator overloading. This is another type of polymorphism where an operator behaves differently based on the type of the operands. <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>+ operator adds two numbers and concatenate two strings<\/li><li>* operator multiplies two numbers and when used with a string and int, repeats the string given int times and concatenate them.<\/li><\/ul>\n\n\n\n<p>Read More at <a href=\"https:\/\/www.askpython.com\/python\/operator-overloading-in-python\">Operator Overloading in Python<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Polymorphism<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li> The codes and classes written once can be reused and implemented multiple times.<\/li><li> It helps in reducing the coupling between different functionalities and behavior of objects.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.askpython.com\/python\/oops\/object-oriented-programming-python\">Python Object-Oriented Programming<\/a><\/li><li>Python Functions<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Polymorphism means having vivid or different forms. In the programming world, Polymorphism refers to the ability of the function with the same name to carry different functionality altogether. It creates a structure that can use many forms of objects. This permits functions\/arguments to use entities of different types at different times. In object-oriented programming, Polymorphism [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-955","post","type-post","status-publish","format-standard","hentry","category-oops"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/955","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=955"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/955\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}