Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python NotImplemented constant

Tags:

python

Looking through decimal.py, it uses NotImplemented in many special methods. e.g.

class A(object):     def __lt__(self, a):         return NotImplemented      def __add__(self, a):         return NotImplemented 

The Python docs say:

NotImplemented

Special value which can be returned by the “rich comparison” special methods (__eq__(), __lt__(), and friends), to indicate that the comparison is not implemented with respect to the other type.

It doesn't talk about other special methods and neither does it describe the behavior.

It seems to be a magic object which if returned from other special methods raises TypeError, and in “rich comparison” special methods does nothing.

e.g.

print A() < A() 

prints True, but

print A() + 1 

raises TypeError, so I am curious as to what's going on and what is the usage/behavior of NotImplemented.

like image 673
Anurag Uniyal Avatar asked Jun 30 '09 06:06

Anurag Uniyal


People also ask

How to use NotImplemented in Python?

If you run into the NotImplementedError, the recommended way to handle it is to implement the abstract method for which the error is being raised. Because the NotImplementedError is user-defined, Python can't raise this error on its own. So, you'll need to raise it by a package you're using or code your team wrote.

What is NotImplemented in Python?

NotImplemented. A special value which should be returned by the binary special methods (e.g. __eq__() , __lt__() , __add__() , __rsub__() , etc.) to indicate that the operation is not implemented with respect to the other type; may be returned by the in-place binary special methods (e.g. __imul__() , __iand__() , etc.)

How do you return a constant in Python?

Another way to create a constant in Python is to create a class and override the __setattr__(self, *_) to return the exception whenever someone tries to assign a new value to the class attribute. We can use a class attribute as a constant in Python in the following way.

What is Python constant?

A constant is a type of variable whose value cannot be changed. It is helpful to think of constants as containers that hold information which cannot be changed later.


1 Answers

NotImplemented allows you to indicate that a comparison between the two given operands has not been implemented (rather than indicating that the comparison is valid, but yields False, for the two operands).

From the Python Language Reference:

For objects x and y, first x.__op__(y) is tried. If this is not implemented or returns NotImplemented, y.__rop__(x) is tried. If this is also not implemented or returns NotImplemented, a TypeError exception is raised. But see the following exception:

Exception to the previous item: if the left operand is an instance of a built-in type or a new-style class, and the right operand is an instance of a proper subclass of that type or class and overrides the base's __rop__() method, the right operand's __rop__() method is tried before the left operand's __op__() method. This is done so that a subclass can completely override binary operators. Otherwise, the left operand's __op__() method would always accept the right operand: when an instance of a given class is expected, an instance of a subclass of that class is always acceptable.

like image 72
Brandon E Taylor Avatar answered Oct 05 '22 10:10

Brandon E Taylor