0% found this document useful (0 votes)
29 views

Unit V - Operator Overloading

Uploaded by

amlakshmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Unit V - Operator Overloading

Uploaded by

amlakshmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1

Python Programming
Using Problem Solving Approach

Reema Thareja

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


CHAPTER 11
Operator Overloading

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


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.

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Implementing Operator Overloading — Example

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Operators and Their Corresponding Function Names

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


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

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Overriding __getitem__() and __setitem__() — Example

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


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:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Overloading Miscellaneous Functions — Example

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


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.

Example:

10

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.

You might also like