Polymorphism

Understanding Polymorphism in Python

Polymorphism, meaning “many forms,” is a concept in programming where methods, functions, or operators with the same name can be applied to different objects or classes. This powerful feature allows methods to operate in a way that is specific to the type of object they are acting upon.

Function Polymorphism

A common example of polymorphism in Python is the len() function, which behaves differently depending on the type of object it is used with:

  • String: The len() function returns the number of characters in a string.
  • Tuple: For tuples, len() returns the number of items contained in the tuple.
  • Dictionary: When used on dictionaries, len() returns the number of key/value pairs present in the dictionary.

Examples:

String Example:

mystr = "The Great Gatsby"
print(len(mystr))

Tuple Example:

mytuple = ("Moby Dick", "Pride and Prejudice", "1984")
print(len(mytuple))

Dictionary Example:

mydict = {
  "title": "To Kill a Mockingbird",
  "author": "Harper Lee",
  "year": 1960
}
print(len(mydict))

Class Polymorphism

Polymorphism extends to class methods as well. Different classes can implement methods with the same name, and the correct method is called based on the class of the object. For instance, consider classes Book, Magazine, and Journal, each having a describe() method:

Example:

class Book:
  def __init__(self, title, author):
    self.title = title
    self.author = author

  def describe(self):
    print("This is a book!")

class Magazine:
  def __init__(self, title, issue):
    self.title = title
    self.issue = issue

  def describe(self):
    print("This is a magazine!")

class Journal:
  def __init__(self, title, volume):
    self.title = title
    self.volume = volume

  def describe(self):
    print("This is a journal!")

book1 = Book("1984", "George Orwell")
magazine1 = Magazine("National Geographic", "June 2024")
journal1 = Journal("Nature", "Volume 593")

for item in (book1, magazine1, journal1):
  item.describe()

The for loop demonstrates polymorphism by calling the describe() method on each object, regardless of its class.

Inheritance and Polymorphism

Polymorphism is also prevalent in inheritance. When a parent class method is overridden in child classes, polymorphism allows the same method name to invoke different implementations based on the child class. Consider a parent class Publication with child classes Book, Magazine, and Journal:

Example:

class Publication:
  def __init__(self, title):
    self.title = title

  def describe(self):
    print("This is a publication!")

class Book(Publication):
  pass

class Magazine(Publication):
  def describe(self):
    print("This is a magazine!")

class Journal(Publication):
  def describe(self):
    print("This is a journal!")

book1 = Book("1984")
magazine1 = Magazine("National Geographic")
journal1 = Journal("Nature")

for item in (book1, magazine1, journal1):
  print(item.title)
  item.describe()

In this example, the Book class inherits the describe() method from Publication without modification, while Magazine and Journal override it. This demonstrates polymorphism as each subclass provides its specific implementation of the method.

Leave a Comment