Introduction Python allows programmers to redefine the meaning of operators when they operate on class objects. This feature is called operator overloading. Operator overloading allows programmers to extend the meaning of existing operators so that in addition to the basic data types, they can be also applied to user defined data types.
Another form of Polymorphism
Like function overloading, operator overloading is also a form of compile-time polymorphism. Operator overloading, is therefore less commonly known as operator ad hoc polymorphism since different operators have different implementations depending on their arguments. Operator overloading is generally defined by the language, the programmer, or both.
Reverse Adding In operator overloading functions, we can add a basic data type on a user defined object by writing user_defined_object + basic_data_type_var but cannot do the reverse. However, to provide greater flexibility, we should also be able to perform the operation in reverse order, that is, adding a non-class object to the class object. For this, Python provides the concept of reverse adding.
For reverse adding, just overload the __radd__() fnction
Overriding the in Operator in is a membership operator that checks whether the specified item is in the variable of built-in type or not We can overload the same operator to check whether the given value is a member of a class variable or not. To overload the in operator we have to use the function __contains__(). Example:
Overriding the __call __() Method The __call__() method is used to overload call expressions. The __call__ method is called automatically when an instance of the class is called. It can be passed any positional or keyword arguments. Like other functions, __call__() also supports all of the argument-passing modes.