Operator Overloading

Overloading operators in Python

Operators in Python can be overloaded to operate in a certain user-defined way. Whenever an operator is used in Python, its corresponding method is invoked to perform its predefined function. For example, when the + operator is called, it invokes the special function, __add__, in Python, but this operator acts differently for different data types. For example, the + operator adds the numbers when it is used between two int data types and merges two strings when it is used between string data types.

Run the code below for the implementation of the + operator for integers and strings.

Get hands-on with 1200+ tech skills courses.