NotImplemented and Reflection Functions in Python

NotImplemented and Reflection Functions in Python

We know that a function or method will: * Return some result, or * Raise an exception if it cannot find the result But there is a third thing a function can return and that is **NotImplemented**. The simple meaning of **return NotImplemented** by...

We know that a function or method will:

  • Return some result, or
  • Raise an exception if it cannot find the result

But there is a third thing a function can return and that is NotImplemented. The simple meaning of return NotImplemented by a function is that the function is declaring that it cannot find the result but instead of raising the exception, it will transfer the control to another function, hoping that the other function will get the result. The other function is known as Reflection Function.

Example case that can be solved using NotImplemented and Reflection Function:

We will explore the whole idea in context to Object Oriented Programming and associated Magic Methods (also known as special methods or dunder methods) which are used for different special functionalities and we will just see the ones used to define the support for different operator (e.g. +,-,* etc.) for objects of one class.

Let's see this simple Point class defined for the points on XY plane:

class Point:
    def __init__(self,x,y):
        self.x=x
        self.y=y
    def __str__(self):
        return f'({self.x},{self.y})'

p1=Point(3,4)
print(p1)

The __str__ method is defined for the proper representation of the point object and the last print statement will print the point as (3,4).

If we want to define a function for the addition of two point objects, it can be done as:

def addPoints(self,other):
        return Point(self.x+other.x,self.y+other.y)

And then we can use it as:

p1=Point(3,4)
p2=Point(1,1)
p3=p1.addPoints(p2)
print(p3)

This will display (4,5) as output.

But it will be better if instead of using a custom named function like addPoints, we could directly apply + operator like we do for simple numbers and a few other data types e.g. 4+5. At present if we use + operator on two point objects as:

p1=Point(3,4)
p2=Point(1,1)
p3=p1+p2

It will generate following error which is self-explanatory:

TypeError: unsupported operand type(s) for +: 'Point' and 'Point'

If we change the name of the function addPoints to __add__ which is a magic method, the + operator applied between two point objects will call this method. See the complete code here:

class Point:
    def __init__(self,x,y):
        self.x=x
        self.y=y
    def __str__(self):
        return f'({self.x},{self.y})'
    def __add__(self,other):
        return Point(self.x+other.x,self.y+other.y)

p1=Point(3,4)
p2=Point(1,1)
p3=p1+p2
print(p3)

And the output will be (4,5).

We have special methods for other operators too e.g. __sub__ is for subtraction ( - ), __mul__ is for multiplication ( * ), __div__ is for division( / ) and a few more.

In fact, when we apply + operator between any data types, interpreter actually executes __add__ method on those e.g. for 3+4, interpreter will execute 3.__add__(4) and the method __add__ is defined in int class for addition of two integers. Similarly, if a and b are two lists, a+b will result into a.__add__(b) and __add__ is defined inside list class as concatenation of the lists.

Now let's define __mul__ for the Point class but not for the multiplication of two point objects but to multiply the Point object with a number, resulting into a new point with scaled x and y components. It will be done by adding __mul__ method as shown here:

class Point:
    def __init__(self,x,y):
        self.x=x
        self.y=y
    def __str__(self):
        return f'({self.x},{self.y})'
    def __add__(self,other):
        return Point(self.x+other.x,self.y+other.y)
    def __mul__(self,num):
        return Point(self.x*num,self.y*num)

p1=Point(3,4)
p2=p1*2
print(p2)

The output will be (6,8).

But instead of p1*2 if we execute 2*p1, then instead of getting the same result, we get an error as:

TypeError: unsupported operand type(s) for *: 'int' and 'Point'

This is something we will resolve using the concept of NotImplemented and Reflection Function.

What happens when a function returns NotImplemented:

When a function returns NotImplemented, interpreter will run the reflection function associated with that function after flipping the input arguments. For example, if original function has input arguments as a and b, at returning NotImplemented, interpreter will run the associated reflection function on b and a.

Second important thing is that the reflection functions associated with different functions are predefined in Python and you cannot make some function as reflection of some other function by your own. The reflection function of __add__ is __radd__, reflection function of __mul__ is __rmul__ and so on for different magic methods.

So, lets see what happened when interpreter executed 2*p1 in above program. As described earlier that 2*p1 will result into 2.__mul__(p1), so interpreter will apply __mul__ on 2 which is an integer. Therefore, interpreter will search for __mul__ method inside int class. Interpreter will find the method in int class but that method defines multiplication between two integers and may be between an integer and some other data type but not between an integer and the Point class object. And in such cases (unsupported datatype), the __mul__ of int class returns NotImplemented. With NotImplemented returned, interpreter will run the reflection method i.e. __rmul__ on flipped input arguments as p1.__rmul__(2). And you can see that the method is applied on p1 which is Point class object and hence interpreter will search for this method (__rmul__) in Point class and will fail since this method is not defined inside Point class. So it will return back to the original function (of int class) and will generate this error:

TypeError: unsupported operand type(s) for *: 'int' and 'Point'

The whole process is given here step by step for further clarification:

  1. 2*p1 will result into 2.__mul__(p1) and Interpreter searches for __mul__ in int class.
  2. __mul__ in int class returns NotImplemeted for unsupported types of operands.
  3. This results into execution of reflection function as p1.__rmul__(2).
  4. Interpreter will look for __rmul__ inside Point class because p1 is Point class object.
  5. When failed to find __rmul__ in Point class, interpreter will go back to int class and generate the error.

So if you have followed along these steps, you probably have figured out the solution which is that we must define __rmul__ in Point class, so that p1.__rmul__(2) should get executed.

What should be inside this function?

p1.__rmul__(2) means that self is p1 and second input argument is 2 and we need to return a new point with scaled xy coordinates. So, we should have __rmul__ defined as:

def __rmul__(self,num):
        return Point(self.x*num,self.y*num)

But this is exactly what __mul__ does, and hence we can also do as:

def __rmul__(self,num):
        return self*num

Above return statement with multiplication will call the __mul__ method. Even a better approach will be to just declare:

__rmul__=__mul__

Because the function are also Objects and we are saying that __rmul__ is referring to same object as __mul__. The complete code will be:

class Point:
    def __init__(self,x,y):
        self.x=x
        self.y=y
    def __str__(self):
        return f'({self.x},{self.y})'
    def __add__(self,other):
        return Point(self.x+other.x,self.y+other.y)
    def __mul__(self,num):
        return Point(self.x*num,self.y*num)
    __rmul__=__mul__
p1=Point(3,4)
p2=2*p1
print(p2)

And now we will get the correct output as (6,8).

You can see that it is just the one last line added in Point class i.e. __rmul__=__mul__ and it resolved a big problem. And that is all because of the power of NotImplemented and Reflection Function support of Python.

Please don’t forget to subscribe: Learning Orbis

You can find further detail with more practice examples in this video:

If you need more detail on Magic Methods, you can find that in the following videos:

notimplemented reflection python programming oop

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Guide to Python Programming Language

Guide to Python Programming Language

OOP in Python | Python OOP Concepts | Object Oriented Programming Python

OOP in Python will help you understand how to write various basic programs in python and real-time uses of some of the Python libraries as well.

Python Programming: A Beginner’s Guide

Python is an interpreted, high-level, powerful general-purpose programming language. You may ask, Python’s a snake right? and Why is this programming language named after it?

Python Hello World Program - Create & Run Your First Python Program in PyCharm

Python Hello World Program - Your first step towards Python world. Learn how to create the Hello World Python program in PyCharm.

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners