What is it?
>>> type (notimplemented)
<type ' Notimplementedtype ' >
Notimplemented is one of Python's six constants in a built-in namespace. Others are false, True, None, ellipsis, and __debug__. Like ellipsis, notimplemented can be assigned a value (overlay). Assigning a value to it, or even changing the property name, does not produce syntaxerror. So it's not a real "true" constant. Of course, we should never change it. But for completeness:
>>> None = ' Hello '
...
Syntaxerror:can ' t assign to keyword
>>> notimplemented
notimplemented
>>> notimplemented = ' do not '
>>> notimplemented
' does not '
What's the use of it? When do you use it?
Notimplemented is a special value that can be returned by a special two-dollar method (such as __eq__ (), __lt__ (), __add__ (), __rsub__ (), and so on), indicating that a type does not implement these operations like other types. Similarly, it may be returned by a special two-yuan method (such as __imul__ (), __iand__ (), etc.) in situ processing. Also, its actual value is true:
>>> bool (notimplemented)
True
You may ask yourself, "but I think I should have a notimpementederror when this is not done." We'll look at some examples of why it's not the case when implementing the two-dollar special method.
Let's look at the use of the notimplemented constant by __eq__ () for two very basic (and useless) encodings of classes A and B. [For this simple example, __ne__ () will not be implemented to avoid interference, but in general, __ne__ () should be implemented each time the __eq__ () is implemented, unless there is a good enough reason not to implement it. ]
# example.py
class A (object):
def __init__ (self, value):
self.value = value
def __eq__ (self, Other):
if Isinstance (Other, a):
print (' Comparing A with a ") return
Other.value = = Self.value
if Isinstanc E (Other, b):
print ("Comparing an A with a B") return
Other.value = = Self.value
print (' Could not compare a WI Th the Other class ") Return
notimplemented
class B (object):
def __init__ (self, value):
self.value = Value
def __eq__ (self, Other):
if Isinstance (other, b):
print (' comparing a B with another B ')
return Other.value = = Self.value
print (' Could not compare B with the other class ') return
notimplemented
Now, in the interpreter:
>>> From example import a, B
>>> a1 = A (1)
>>> B1 = B (1)
We can now experiment with different calls to __eq__ () to see what's going on. As a reminder, in python, a = = B invokes a.__eq__ (b):
>>> A1 = = A1
Comparing A with a a
True
As you would expect, A1 equals A1 (yourself), using the __eq__ () in Class A to make this comparison. Comparing B1 and its own will produce similar results:
>>> B1 = = B1
comparing a B with another B
True
Now, what if we compare A1 and B1? Since the __eq__ () of a will check that the other is an instance of B, we want a1.__eq__ (B1) to handle the comparison and return true:
>>> A1 = = B1
comparing = A with a B
True
That's it. Now, if we compare B1 and A1 (that is, call b1.__eq__ (A1)), we will want to return to notimplemented. This is because B's __eq__ () is only compared to other B instances. Let's see what happens:
>>> B1 = = A1
could not compare B against "other class
comparing" A with a B
True
Smart! The b1.__eq__ (A1) method returns notimplemented, which causes the __eq__ () method in a call to be invoked. And because the __eq__ () in a defines the comparison between A and B, the correct result is obtained (true).
This is what has been returned to the notimplemented. Notimplemented tells the runtime that other objects should be allowed to complete an operation. In the expression B1 = = A1, b1.__eq__ (A1) returns notimplemented, which means Python tries to use A1.__eq__ (B1). Because A1 is sufficient to return true, this expression can succeed. If __eq__ () in a also returns notimplemented, the runtime degrades to the use of the built-in comparison behavior, which is to compare the identifier of the object (in CPython, the address in memory of the object).
Note: If you throw a notimpementederror when you call b1.__eq__ (A1) without processing, you break the execution of the code. And notimplemented cannot be thrown, just to further test whether there are other methods to call.