Operator Overloading in Python

In this article, we’ll learn about operator overloading in Python with examples.

We all know what are operators (+, -, <=). In python, operators work for built in classes, but some operator behaves differently with different types. For example ‘+’ operator can add two numbers and also can concatenate two strings.

Program:

Output:

30

Program:

 

Output:

hello programmer”

 

So, using any operator to perform different operation that are not usually performed, is known as operator overloading. We can change the behavior of operators using operator overloading.

Or we can also say that “assigning a different work to an operator is known as operator overloading”.

To perform operator overloading, there are some magic methods provided by Python. Using these methods we can perform any operation we want on a operator.

The operators that can be overloaded are as follows:

OperatorsMethods
+__add__(self, other)
__sub__(self, other)
*__mul__(self, other)
//__floordiv__(self, other)
/__div__(self, other)
%__mod__(self, other)
**__pow__(self, other[ , modulo])
<__lt__(self, other)
<=__le__(self, other)
==__eq__(self, other)
!=__ne__(self , other)
>=__ge__(self, other)

The basic idea to perform the operator overloading in python is to define any of these methods in the class then call them using operators.

Let’s see an example. Suppose we want to overload  ‘+’ operator.

As mentioned above that we can concatenate two strings and add two numbers with the help of ‘+’ operator but here we’ll perform the addition on two objects of a class named as Test.

 

Program:

Output:

TypeError: unsupported operand type(s) for +: ‘Test’ and ‘Test’

So there is an error that says that we can’t perform addition to add values of the both the Test class’s objects. So we will define the __add___(self, other)  method  here to add the values of both the objects.

Program:

 

Output:

11

So what we did in above program is just added a __add__(self, other) method that will be called when we use ‘+’ operator on the objects of the Test class.

It is not necessary to always perform addition on ‘+’ operator we can do anything we want like we can also subtract object’s values by using operator ‘+’.

In this way you can achieve operator overloading in python. Comment below if you have any doubts.

Some Popular Python GUI Libraries

Here we will talk about some of the popular Python GUI Libraries, we will compare the three major GUI libraries their positives and negatives.

Let’s look at some of the most popular options available to Python developers and see how they stack up. All three are cross-platform and can be used on your operating system of choice.

Eclipse with PyDev

It’s hard to write anything about open source integrated development environments without covering Eclipse, which has a huge developer community and countless plugins available allowing you to customize it to meet nearly any need you can imagine. But this kitchen sink approach is also one of Eclipse’s downsides. Many criticize it as bloated, and performance on low spec systems certainly can be an issue.

That said, if you’re coming to Python from a background in a different language, particularly Java, Eclipse may already be your go to IDE. And if you make use of its many features, you may find life without them difficult.

PyDev adds a huge number of features to Eclipse, far beyond simple code highlighting. It handles code completion, integrates Python debugging, adds a token browser, refactoring tools, and much more. For those working with the popular Django Python web framework, PyDev will allow you to create new Django projects, execute Django actions via hotkeys, and use a separate run configuration just for Django.

Eclipse and PyDev are both made available under the Eclipse Public License

PYDEV Python GUI Libraries

 

Eric

Eric is my personal favorite IDE for Python editing. Named after Monty Python’s Eric Idle, Eric is actually written in Python using the Qt framework.

Eric makes use of Scintilla, a source code editing component which is used in a number of different IDEs and editors which is also available as the stand-alone SciTE editor.

The features of Eric are similar to other IDEs: brace matching, code completion, a class browser, integrated unit tests, etc. It also has a Qt form preview function, which is useful if you’re developing a Qt GUI for your application, and I personally like the integrated task list function.

I’ve heard some criticisms of Eric’s documentation, which primarily being delivered through a massive PDF does leave something to be desired, but if you take the time to learn it, I find Eric to be a lighweight yet full-featured programming environment.

Eric is made available under the GPL version 3.

 

PyCharm

PyCharm is another popular Python editor and rounds out my top three. Pycharm is a commercial product, but the makers also offer a community edition which is free and open source under the Apache 2.0 license.

PyCharm features pretty much everything one might hope for in an IDE: integrated unit testing, code inspection, integrated version control, code refactoring tools, a variety of tools for project navigation, as well as the highlighting and automated completion features you would expect with any IDE.

To me, the main drawback of PyCharm is its open core model. Many of PyCharm’s advanced features are not available under an open source license, and for me, that’s a deal breaker. However, if you’re not looking to use the more advanced features included in the closed source verion, having the features left out may leave PyCharm as a lighter weight choice for Python editing.

PYCHARM Python GUI Libraries

Other great options for Python GUI Libraries

The list of open source Python editors and integrated development environments is lengthy. Here are a few other interesting standouts.

  • PyScripter, LeoEditor, and PTK (the Python Tool Kit) are all lesser-known tools for working with Python code. And of course there’s IDLE, the default IDE packaged with Python.
  • Bluefish and Geany are two great general purpose IDEs with strong Python support.
  • Spyder is an IDE specifically designed for working with scientific Python development and the libraries commonly associated with this type of work.

Please comment for any concerns or suggestions.

Python Program to Find the Union of two Lists

Here we will write a program in Python to find union of two lists. We will write a user defined function to enter the lists and will display the union as output.

Step-wise Solution:

1. Define a function which accepts two lists and returns the union of them.
2. Declare two empty lists and initialise to an empty list.
3. Consider a for loop to accept values for two lists.
4. Take the number of elements in the list and store it in a variable.
5. Accept the values into the list using another for loop and insert into the list.
6. Repeat 4 and 5 for the second list also.
7. Find the union of the two lists.
8. Print the union.
9. Exit.

Now lets write the code for the same.

Python Program to Find the Union of two Lists

OUTPUT:

Please comment for any suggestion or concerns.

Python Program to Find Perfect Number

Here we will write a program in Python to check whether the number is a Perfect number. So, first lets start with what is a perfect number.

What is a Perfect number:

Perfect Number is a number in which sum of all its positive divisors excluding that number is equals to the number.
For example:     6
Sum of divisors = 1+2+3 = 6

Step-wise Solution:

1. Take in an integer and store it in a variable.
2. Initialize a variable to count the sum of the proper divisors to 0.
3. Use a for loop and an if statement to add the proper divisors of the integer to the sum variable.
4. Check if the sum of the proper divisors of the number is equal to the variable.
5. Print the final result.
6. Exit.

Now lets write the code for the same.

Python Program to Find Perfect Number

OUTPUT:

Please comment for any suggestion or concerns.

Python Program to Find the Sum of Digits of a Number

Here we will write a program in Python to find the sum of Digits of a Number provided using while loop. Lets start with the steps for solution.

Step-wise Solution:

1. Take the value of the integer and store in a variable.
2. Using a while loop, get each digit of the number and add the digits to a variable.
3. Print the sum of the digits of the number.
4. Exit.

Now lets write the code for the same.

Python Program to Find the Sum of Digits of a Number:

 

OUTPUT:

Comment for any suggestions, concerns.

Python Program to Reverse a number

Here we will write a program in Python to Reverse a number using while loop. Lets start with the steps:

Step-wise Solution:

1. Take the value of the integer and store in a variable.
2. Using a while loop, get each digit of the number and store the reversed number in another variable.
3. Print the reverse of the number.
4. Exit.

Now lets write the program for the same.

Python Program to Reverse a number:

OUTPUT:

Comment for any suggestion or concerns.