For Enquiry: 93450 45466

Method Overloading In Python



Let’s understand Method Overloading In Python deeply with this table of content…

What is Overloading in Python
Operator Overloading With Examples
How To Overload Method In Python 
Example Programs For Method Overloading
Method Overriding
Example Programs For Method Overriding

Let us now learn the Method Of Overloading in Python, starting with what is Overloading In Python.

What Is Overloading In Python?

Overloading is giving different definitions to a method or function in different places i.e., the method will work differently but with the same name.

Overloading the method, prioritizes the DRY(Don’t Repeat Yourself) rule, by code redundancy, reusability. Overloading avoids complexities in code and improves code clarity.

After understanding what is Overloading in python, let us now see what is operator overloading in python along with example programs.

Operator Overloading With Examples

In python, the operator behaves differently based on the type of object, this also a type of polymorphism, but as operator overloading.

For example

+ (operator) sums up two numbers or concatenates the two or more in case of strings.

>> 3+3

6

>> ‘3’+’3′

33

* multiplies two numbers or repeats the object in case of use between strings.

>> 3*3

9

>> ‘3’*3

333

After understanding what is Operator Overloading in python, let us now see how to overload a method in python along with an example program.

How to Overload Method In Python

In python, you can overload the same method or function defined in a place, with a different number of arguments or zero arguments while using the same name at different places. This process is known as Method Overloading.

Example Program For Method Overloading

class Car:

     def properties(self, name=None):

         if name is not None:

               print(‘This is the brand new’ + name)

         else: 

               print(‘The car has not been named yet!’)

# Creating instance

obj = Car()

# Calling the method without parameter

obj.properties()

 

The above program outputs

The car has not been named yet!

Now if we try to pass an argument to the object for the same function 

# Calling the method with a parameter

obj.properties(‘FITA’)

And it outputs…

This is FITA

Check out this complete Online Python Course by FITA. FITA provides a complete Python course which covers all the basics and advanced concepts of python including exception handlings, regular expressions, along with building real-time projects like Bitly and Twitter with Django, bundled with placement support, and certification at an affordable price with an active placement cell, to make you industry required certified developer.

After understanding how to Overloading a method in python, let us now see what is method overriding method in python along with an example program.

Method Overriding

Method overriding is when you are using the same method name, and the same number of arguments in the classes and usually in case of inheritance.

Let’s say you want to give different discounts to your regular and occasional customers because regular customers know your previous prices well and are good at a bargain, although you can convince occasional customers with a less discount.

Here’s a simple code that will have the same functionalities but different discount rate,

# Example program for Method Overriding


class RegularCustomer:


       def __init__(self, name, item, price, quantity):

           self.name = name

           self.item = item

           self.price = price

           self.quantity = quantity


     def discount(self):

          return self.price * 0.3


class OccasionalCustomer(RegularCustomer):

    def __init__(self, name, item, price, quantity):

       super().__init__(name, item, price, quantity)


   def discount(self):

       return self.price * 0.1


# common interface

def cal_discount(object):

    z = object.discount()

    print(f'{object.name}\’s discount will be {z}’)

    print(f'{object.final()} is your final price’)

The above program has two different classes RegularCustomer and OccasionalCustomer.The __init__ method also known as the dunder or magic method is a constructor for the class and initializes the attributes to be used within the class.

The super() charge method and the subclasses initializes all the attributes passed and all the methods of the base class into the child class.

A discount method defined above for the regular customers calculate a 30 percent discount, whereas it has been defined again in the child class to return a 10 percent discount. This is method overriding, if we wouldn’t have defined the discount method again, it will calculate a 30 percent discount for both the types of customers.

Let’s test the program now by creating objects of this class and calling methods,

cus_1 = RegularCustomer(‘Atufa’, ‘shoes’, 600, 1)

cus_2 = OccasionalCustomer(‘Shireen’, ‘shoes’, 600, 1)


# passing the objects to common interface

print(cal_discount(cus_1))

print(cal_discount(cus_2))

The above program will now output

Atufa’s discount will be 180.0

Shireen’s discount will be 60.0

Let’s now add one more method which will give us the final price of the program.

Add this method only in the base class and you will be good to go..

def final(self):

return (self.price – self.discount()) * self.quantity

This will now output as follows

Atufa’s discount will be 180.0

420.0 is Atufa final price


Shireen’s discount will be 60.0

540.0 is Shireen final price

You can now leave the first print function to see the discount and leave it to your program.

You can find this complete code at my Github account over here.

This was all about what is method overloading and method overriding and how to implement it along with example programs. To get in depth knowledge of Python along with its various applications and real-time projects, you can enroll in Python Training in Chennai or Python Course in Bangalore by FITA which covers all the basics and advanced concepts of python including exception handlings, regular expressions, along with building real-time projects like Bitly and Twitter with Django, at an affordable price, which includes certification, support, and career guidance assistance with an active placement cell, to make you industry required certified python developer.

FITA’s courses training is delivered by professional experts who have worked in the software development and testing industry for a minimum of 10+ years, and have experience of working with different software frameworks and software testing designs.





  • Trending Courses

    JAVA Training In Chennai Software Testing Training In Chennai Selenium Training In Chennai Python Training in Chennai Data Science Course In Chennai Digital Marketing Course In Chennai DevOps Training In Chennai German Classes In Chennai Artificial Intelligence Course in Chennai AWS Training in Chennai UI UX Design course in Chennai Tally course in Chennai Full Stack Developer course in Chennai Salesforce Training in Chennai ReactJS Training in Chennai CCNA course in Chennai Ethical Hacking course in Chennai RPA Training In Chennai Cyber Security Course in Chennai IELTS Coaching in Chennai Graphic Design Courses in Chennai Spoken English Classes in Chennai Data Analytics Course in Chennai

    Spring Training in Chennai Struts Training in Chennai Web Designing Course In Chennai Android Training In Chennai AngularJS Training in Chennai Dot Net Training In Chennai C / C++ Training In Chennai Django Training in Chennai PHP Training In Chennai iOS Training In Chennai SEO Training In Chennai Oracle Training In Chennai Cloud Computing Training In Chennai Big Data Hadoop Training In Chennai UNIX Training In Chennai Core Java Training in Chennai Placement Training In Chennai Javascript Training in Chennai Hibernate Training in Chennai HTML5 Training in Chennai Photoshop Classes in Chennai Mobile Testing Training in Chennai QTP Training in Chennai LoadRunner Training in Chennai Drupal Training in Chennai Manual Testing Training in Chennai WordPress Training in Chennai SAS Training in Chennai Clinical SAS Training in Chennai Blue Prism Training in Chennai Machine Learning course in Chennai Microsoft Azure Training in Chennai Selenium with Python Training in Chennai UiPath Training in Chennai Microsoft Dynamics CRM Training in Chennai VMware Training in Chennai R Training in Chennai Automation Anywhere Training in Chennai GST Training in Chennai Spanish Classes in Chennai Japanese Classes in Chennai TOEFL Coaching in Chennai French Classes in Chennai Informatica Training in Chennai Informatica MDM Training in Chennai Big Data Analytics courses in Chennai Hadoop Admin Training in Chennai Blockchain Training in Chennai Ionic Training in Chennai IoT Training in Chennai Xamarin Training In Chennai Node JS Training In Chennai Content Writing Course in Chennai Advanced Excel Training In Chennai Corporate Training in Chennai Embedded Training In Chennai Linux Training In Chennai Oracle DBA Training In Chennai PEGA Training In Chennai Primavera Training In Chennai Tableau Training In Chennai Spark Training In Chennai Appium Training In Chennai Soft Skills Training In Chennai JMeter Training In Chennai Power BI Training In Chennai Social Media Marketing Courses In Chennai Talend Training in Chennai HR Courses in Chennai Google Cloud Training in Chennai SQL Training In Chennai CCNP Training in Chennai PMP Training in Chennai OET Coaching Centre in Chennai Business Analytics Course in Chennai NextJS Course in Chennai Vue JS Course in Chennai

  • Read More Read less
  • Are You Located in Any of these Areas

    Adambakkam, Adyar, Akkarai, Alandur, Alapakkam, Alwarpet, Alwarthirunagar, Ambattur, Ambattur Industrial Estate, Aminjikarai, Anakaputhur, Anna Nagar, Anna Salai, Arumbakkam, Ashok Nagar, Avadi, Ayanavaram, Besant Nagar, Bharathi Nagar, Camp Road, Cenotaph Road, Central, Chetpet, Chintadripet, Chitlapakkam, Chengalpattu, Choolaimedu, Chromepet, CIT Nagar, ECR, Eechankaranai, Egattur, Egmore, Ekkatuthangal, Gerugambakkam, Gopalapuram, Guduvanchery, Guindy, Injambakkam, Irumbuliyur, Iyyappanthangal, Jafferkhanpet, Jalladianpet, Kanathur, Kanchipuram, Kandhanchavadi, Kandigai, Karapakkam, Kasturbai Nagar, Kattankulathur, Kattupakkam, Kazhipattur, Keelkattalai, Kelambakkam, Kilpauk, KK Nagar, Kodambakkam, Kolapakkam, Kolathur, Kottivakkam, Kotturpuram, Kovalam, Kovilambakkam, Kovilanchery, Koyambedu, Kumananchavadi, Kundrathur, Little Mount, Madambakkam, Madhavaram, Madipakkam, Maduravoyal, Mahabalipuram, Mambakkam, Manapakkam, Mandaveli, Mangadu, Mannivakkam, Maraimalai Nagar, Medavakkam, Meenambakkam, Mogappair, Moolakadai, Moulivakkam, Mount Road, MRC Nagar, Mudichur, Mugalivakkam, Muttukadu, Mylapore, Nandambakkam, Nandanam, Nanganallur, Nanmangalam, Narayanapuram, Navalur, Neelankarai, Nesapakkam, Nolambur, Nungambakkam, OMR, Oragadam, Ottiyambakkam, Padappai, Padi, Padur, Palavakkam, Pallavan Salai, Pallavaram, Pallikaranai, Pammal, Parangimalai, Paruthipattu, Pazhavanthangal, Perambur, Perumbakkam, Perungudi, Polichalur, Pondy Bazaar, Ponmar, Poonamallee, Porur, Pudupakkam, Pudupet, Purasaiwakkam, Puzhuthivakkam, RA Puram, Rajakilpakkam, Ramapuram, Red Hills, Royapettah, Saidapet, Saidapet East, Saligramam, Sanatorium, Santhome, Santhosapuram, Selaiyur, Sembakkam, Semmanjeri, Shenoy Nagar, Sholinganallur, Singaperumal Koil, Siruseri, Sithalapakkam, Srinivasa Nagar, St Thomas Mount, T Nagar, Tambaram, Tambaram East, Taramani, Teynampet, Thalambur, Thirumangalam, Thirumazhisai, Thiruneermalai, Thiruvallur, Thiruvanmiyur, Thiruverkadu, Thiruvottiyur, Thoraipakkam, Thousand Light, Tidel Park, Tiruvallur, Triplicane, TTK Road, Ullagaram, Urapakkam, Uthandi, Vadapalani, Vadapalani East, Valasaravakkam, Vallalar Nagar, Valluvar Kottam, Vanagaram, Vandalur, Vasanta Nagar, Velachery, Vengaivasal, Vepery, Vettuvankeni, Vijaya Nagar, Villivakkam, Virugambakkam, West Mambalam, West Saidapet

    FITA Velachery or T Nagar or Thoraipakkam OMR or Anna Nagar or Tambaram or Porur or Pallikaranai branch is just few kilometre away from your location. If you need the best training in Chennai, driving a couple of extra kilometres is worth it!