Python Overloading

Some operators and other elements of the Python programming language can show overloading. Here is all you need to know about Python Overloading.

Python overloading: Operator overloading

Below are the operators that can be overloaded in classes, along with the method definitions that are required, and an example of the operator in use within an expression.

N.B. The use of other as a variable name is not mandatory, but is considered the norm.

            Operator    Method      Expression
    Addition                                                                            

add(self, other) a1 + a2
Subtraction

sub(self, other) a1 – a2
Multiplication

mul(self, other) a1 * a2
Matrix Multiplication (Python 3.5)
@ matmul(self, other)
a1 @ a2
Division (Python 2 only)
/ div(self, other)
a1 / a2
Division (Python 3)
/ truediv(self, other)
a1 / a2
Floor Division
// floordiv(self, other) a1 // a2
Modulo/Remainder
% mod(self, other) a1 % a2
Power
** pow(self, other[, modulo]) a1 ** a2
Bitwise Left Shift
<< lshift(self, other) a1 << a2
Bitwise Right Shift
   __rshift__(self, other)             a1  >> a2           
Bitwise AND                                             
& and(self, other) a1 & a2
Bitwise XOR
^ xor(self, other) a1 ^ a2
(Bitwise OR)
| or(self, other) a1 | a2
Negation (Arithmetic)

neg(self) -a1
Positive

pos(self) +a1
Bitwise NOT
~ invert(self) ~a1
Less than
< lt(self, other) a1 < a2 Less than or Equal to <= le(self, other) a1 <= a2 Equal to == eq(self, other) a1 == a2 Not Equal to != ne(self, other) a1 != a2 Greater than gt(self, other) a1 > a2
Greater than or Equal to
= ge(self, other) a1 >= a2
Index operator
[index] getitem(self, index) a1[index]
In operator
in contains(self, other) a2 in a1
Calling
(*args, …) call(self, *args, *kwargs) a1(args, **kwargs)
The optional parameter modulo for pow is only used by the pow built-in function.

Each of the methods corresponding to a binary operator has a corresponding “right” method which start with r, for example __radd:

class A:
def init(self, a):
self.a = a
def add(self, other):
return self.a + other
def radd(self, other):
print("radd")
return other + self.a
A(1) + 2 # Out: 3
2 + A(1) # prints radd. Out: 3

as well as a corresponding inplace version, starting with __i:

class B:
def init(self, b):
self.b = b
def iadd(self, other):
self.b += other
print("iadd")
return self
b = B(2)

Out: 2

b += 1 # prints iadd

Out: 3

Since there’s nothing special about these methods, many other parts of the language, parts of the standard library, and even third-party modules add magic methods on their own, like methods to cast an object to a type or checking properties of the object. For example, the builtin str() function calls the object’s str method, if it exists. Some of these uses are listed below.

Function Method Expression
Casting to
int int(self) int(a1)
Absolute function
abs(self) abs(a1)
Casting to
str str(self) str(a1)
Casting to (Python 2 only)
unicode(self)
unicode unicode(a1)
String representation
repr(self) repr(a1)
Casting to
bool nonzero(self) bool(a1)
String formatting
format(self, formatstr) "Hi {:abc}".format(a1)
Hashing
hash(self) hash(a1)
Length
len(self) len(a1)
Reversed
reversed(self) reversed(a1)
Floor
floor(self) math.floor(a1)
Ceiling
ceil(self) math.ceil(a1)
There are also the special methods enter and exit for context managers, and many more.

Python overloading: Magic/Dunder Methods

Magic (also called dunder as an abbreviation for double-underscore) methods in Python serve a similar purpose to operator overloading in other languages. They allow a class to define its behavior when it is used as an operand in unary or binary operator expressions. They also serve as implementations called by some built-in functions.

Consider this implementation of two-dimensional vectors.

import math
class Vector(object):

instantiation

def init(self, x, y):
self.x = x
self.y = y
unary negation (-v) def neg(self):
return Vector(-self.x, -self.y)
addition (v + u)
def add(self, other):
return Vector(self.x + other.x, self.y + other.y)

subtraction (v – u)

def sub(self, other):
return self + (-other)

equality (v == u)

def eq(self, other):
return self.x == other.x and self.y == other.y

abs(v)

def abs(self):
return math.hypot(self.x, self.y)

str(v)

def str(self):
return '<{0.x}, {0.y}>'.format(self)

repr(v)

def repr(self):
return 'Vector({0.x}, {0.y})'.format(self)

Now it is possible to naturally use instances of the Vector class in various expressions.

v = Vector(1, 4)
u = Vector(2, 0)
u + v # Vector(3, 4)
print(u + v) # "<3, 4>" (implicit string conversion)
u - v # Vector(1, -4)
u == v # False
u + v == v + u # True
abs(u + v) # 5.0

Python overloading: Container and sequence types

It is possible to emulate container types, which support accessing values by key or index.

Consider this naive implementation of a sparse list, which stores only its non-zero elements to conserve memory.

class sparselist(object):
def init(self, size):
self.size = size
self.data = {}
l[index]
def getitem(self, index):
if index < 0: index += self.size if index >= self.size:
raise IndexError(index)
try:
return self.data[index]
except KeyError:
return 0.0

l[index] = value

def setitem(self, index, value):
self.data[index] = value

del l[index]

def delitem(self, index):
if index in self.data:
del self.data[index]

value in l

def contains(self, value):
return value == 0.0 or value in self.data.values()

len(l)

def len(self):
return self.size
for value in l: …
def iter(self):
return (self[i] for i in range(self.size)) # use xrange for python2

Then, we can use a sparselist much like a regular list.

l = sparselist(10 ** 6) # list with 1 million elements
0 in l # True
10 in l # False
l[12345] = 10
10 in l # True
l[12345] # 10
for v in l:
pass # 0, 0, 0, … 10, 0, 0 … 0

Callable types

class adder(object):
def init(self, first):
self.first = first

a(…)

def call(self, second):
return self.first + second
add2 = adder(2)
add2(1) # 3
add2(2) # 4

Python overloading: Handling unimplemented behaviour

If your class doesn’t implement a specific overloaded operator for the argument types provided, it should return NotImplemented (note that this is a special constant, not the same as NotImplementedError). This will allow Python to fall back to trying other methods to make the operation work:

When NotImplemented is returned, the interpreter will then try the reflected operation on the other type, or some other fallback, depending on the operator. If all attempted operations return NotImplemented, the interpreter will raise an appropriate exception.

For example, given x + y, if x.add(y) returns unimplemented, y.radd(x) is attempted instead.

class NotAddable(object):
def init(self, value):
self.value = value
def add(self, other):
return NotImplemented
class Addable(NotAddable):
def add(self, other):
return Addable(self.value + other.value)
radd = add

As this is the reflected method we have to implement add and radd to get the expected behaviour in all cases; fortunately, as they are both doing the same thing in this simple example, we can take a shortcut.

In use:

x = NotAddable(1)
y = Addable(2)
x + x
Traceback (most recent call last):
File "", line 1, in
TypeError: unsupported operand type(s) for +: 'NotAddable' and 'NotAddable'
y + y

z = x + y
z

z.value
3

Learn More

Must Read Python Interview Questions

200+ Python Tutorials With Coding Examples

Python Language Basics TutorialPython String Representations of Class Instances
Python For Beginners TutorialPython Debugging Tutorial
Python Data Types TutorialReading and Writing CSV File Using Python
Python Indentation TutorialWriting to CSV in Python from String/List
Python Comments and Documentation TutorialPython Dynamic Code Execution Tutorial
Python Date And Time TutorialPython Code Distributing using Pyinstaller
Python Date Formatting TutorialPython Data Visualization Tutorial
Python Enum TutorialPython Interpreter Tutorial
Python Set TutorialPython Args and Kwargs
Python Mathematical Operators TutorialPython Garbage Collection Tutorial
Python Bitwise Operators TutorialPython Pickle Data Serialisation
Python Bolean Operators TutorialPython Binary Data Tutorial
Python Operator Precedance TutorialPython Idioms Tutorial
Python Variable Scope And Binding TutorialPython Data Serialization Tutorial
Python Conditionals TutorialPython Multiprocessing Tutorial
Python Comparisons TutorialPython Multithreading Tutorial
Python Loops TutorialPython Processes and Threads
Python Arrays TutorialPython Concurrency Tutorial
Python Multidimensional Arrays TutorialPython Parallel Computation Tutorial
Python List TutorialPython Sockets Module Tutorial
Python List Comprehensions TutorialPython Websockets Tutorial
Python List Slicing TutorialSockets Encryption Decryption in Python
Python Grouby() TutorialPython Networking Tutorial
Python Linked Lists TutorialPython http Server Tutorial
Linked List Node TutorialPython Flask Tutorial
Python Filter TutorialIntroduction to Rabbitmq using Amqpstorm Python
Python Heapq TutorialPython Descriptor Tutorial
Python Tuple TutorialPython Tempflile Tutorial
Python Basic Input And Output TutorialInput Subset and Output External Data Files using Pandas in Python
Python Files And Folders I/O TutorialUnzipping Files in Python Tutorial
Python os.path TutorialWorking with Zip Archives in Python
Python Iterables And Iterators Tutorialgzip in Python Tutorial
Python Functions TutorialStack in Python Tutorial
Defining Functions With List Arguments In PythonWorking with Global Interpreter Lock (GIL)
Functional Programming In PythonPython Deployment Tutorial
Partial Functions In PythonPython Logging Tutorial
Decorators Function In PythonPython Server Sent Events Tutorial
Python Classes TutorialPython Web Server Gateway Interface (WSGI)
Python Metaclasses TutorialPython Alternatives to Switch Statement
Python String Formatting TutorialPython Packing and Unpacking Tutorial
Python String Methods TutorialAccessing Python Sourcecode and Bytecode
Using Loops Within Functions In PythonPython Mixins Tutorial
Python Importing Modules TutorialPython Attribute Access Tutorial
Difference Betweeb Module And Package In PythonPython Arcpy Tutorial
Python Math Module TutorialPython Abstract Base Class Tutorial
Python Complex Math TutorialPython Plugin and Extension Classes
Python Collections Module TutorialPython Immutable Datatypes Tutorial
Python Operator Module TutorialPython Incompatibilities Moving from Python 2 to Python 3
Python JSON Module TutorialPython 2to3 Tool Tutorial
Python Sqlite3 Module TutorialNon-Official Python implementations
Python os Module TutorialPython Abstract Syntax Tree
Python Locale Module TutorialPython Unicode and Bytes
Python Itertools Module TutorialPython Serial Communication (pyserial)
Python Asyncio Module TutorialNeo4j and Cypher using Py2Neo
Python Random Module TutorialBasic Curses with Python
Python Functools Module TutorialTemplates in Python
Python dis Module TutorialPython Pillow
Python Base64 Module TutorialPython CLI subcommands with precise help output
Python Queue Module TutorialPython Database Access
Python Deque Module TutorialConnecting Python to SQL Server
Python Webbrowser Module TutorialPython and Excel
Python tkinter TutorialPython Turtle Graphics
Python pyautogui Module TutorialPython Persistence
Python Indexing And Slicing TutorialPython Design Patterns
Python Plotting With Matplotlib TutorialPython hashlib
Python Graph Tool TutorialCreating a Windows Service Using Python
Python Generators TutorialMutable vs Immutable (and Hashable) in Python
Python Reduce TutorialPython configparser
Python Map Function TutorialPython Optical Character Recognition
Python Exponentiation TutorialPython Virtual Environments
Python Searching TutorialPython Virtual Environment – virtualenv
Sorting Minimum And Maximum In PythonPython Virtual environment with virtualenvwrapper
Python Print Function TutorialCreate virtual environment with virtualenvwrapper in windows
Python Regular Expressions Regex TutorialPython sys Tutorial
Copying Data In Python TutorialChemPy – Python package
Python Context Managers (“with” Statement) TutorialPython pygame
Python Name Special Variable TutorialPython pyglet
Checking Path Existence And Permissions In PythonWorking with Audio in Python
Creating Python Packages TutorialPython pyaudio
Usage of pip Module In Python TutorialPython shelve
Python PyPi Package Manager TutorialIoT Programming with Python and Raspberry PI
Parsing Command Line Arguments In Pythonkivy – Cross-platform Python Framework for NUI Development
Python Subprocess Library TutorialPandas Transform
Python setup.py TutorialPython vs. JavaScript
Python Recursion TutorialCall Python from C#
Python Type Hints TutorialPython Writing Extensions
Python Exceptions TutorialPython Lex-Yacc
Raise Custom Exceptions In PythonPython Unit Testing
Python Commonwealth Exceptions TutorialPython py.test
Python urllib TutorialPython Profiling
Web Scraping With Python TutorialPython Speed of Program
Python HTML Parsing TutorialPython Performance Optimization
Manipulating XML In PythonPython Security and Cryptography
Python Requests Post TutorialSecure Shell Connection in Python
Python Distribution TutorialPython Anti Patterns
Python Property Objects TutorialPython Common Pitfalls
Python Overloading TutorialPython Hidden Features
Python Polymorphism TutorialPython For Machine Learning
Python Method Overriding TutorialPython Interview Questions And Answers For Experienced
Python User Defined Methods TutorialPython Coding Interview Questions And Answers
Python Programming Tutorials With Examples

Other Python Tutorials

Leave a Comment