1598465520

# 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 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)
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})'
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})'
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})'
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

## Buddha Community  1619510796

## Lambda, Map, Filter functions in python

Welcome to my Blog, In this article, we will learn python lambda function, Map function, and filter function.

Lambda function in python: Lambda is a one line anonymous function and lambda takes any number of arguments but can only have one expression and python lambda syntax is

Syntax: x = lambda arguments : expression

Now i will show you some python lambda function examples:

#python #anonymous function python #filter function in python #lambda #lambda python 3 #map python #python filter #python filter lambda #python lambda #python lambda examples #python map 1619518440

## top 30 Python Tips and Tricks for Beginners

Welcome to my Blog , In this article, you are going to learn the top 10 python tips and tricks.

### 8) Check The Memory Usage Of An Object.

#python #python hacks tricks #python learning tips #python programming tricks #python tips #python tips and tricks #python tips and tricks advanced #python tips and tricks for beginners #python tips tricks and techniques #python tutorial #tips and tricks in python #tips to learn python #top 30 python tips and tricks for beginners 1602968400

## Python Tricks Every Developer Should Know

Python is awesome, it’s one of the easiest languages with simple and intuitive syntax but wait, have you ever thought that there might ways to write your python code simpler?

In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.

### Let’s get started

Swapping value in Python

Instead of creating a temporary variable to hold the value of the one while swapping, you can do this instead

``````>>> FirstName = "kalebu"
>>> LastName = "Jordan"
>>> FirstName, LastName = LastName, FirstName
>>> print(FirstName, LastName)
('Jordan', 'kalebu')
``````

#python #python-programming #python3 #python-tutorials #learn-python #python-tips #python-skills #python-development 1602666000

## How to Remove all Duplicate Files on your Drive via Python

Today you’re going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates.

### Intro

In many situations you may find yourself having duplicates files on your disk and but when it comes to tracking and checking them manually it can tedious.

Heres a solution

But How do we do it?

If we were to read the whole file and then compare it to the rest of the files recursively through the given directory it will take a very long time, then how do we do it?

The answer is hashing, with hashing can generate a given string of letters and numbers which act as the identity of a given file and if we find any other file with the same identity we gonna delete it. There’s a variety of hashing algorithms out there such as

• md5
• sha1
• sha224, sha256, sha384 and sha512

#python-programming #python-tutorials #learn-python #python-project #python3 #python #python-skills #python-tips 1597751700

## How To Compare Tesla and Ford Company By Using Magic Methods in Python

Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘<’, ‘>’, ‘==’, ‘+’ etc…

You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like init, call, str etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).

Now there are a number of such special methods, which you might have come across too, in Python. We will just be taking an example of a few of them to understand how they work and how we can use them.

### 1. init

``````class AnyClass:
def __init__():
print("Init called on its own")
obj = AnyClass()
``````

The first example is _init, _and as the name suggests, it is used for initializing objects. Init method is called on its own, ie. whenever an object is created for the class, the init method is called on its own.

The output of the above code will be given below. Note how we did not call the init method and it got invoked as we created an object for class AnyClass.

``````Init called on its own
``````

Let’s move to some other example, add gives us the ability to access the built in syntax feature of the character +. Let’s see how,

``````class AnyClass:
def __init__(self, var):
self.some_var = var