Writing code today, inadvertently encountered notimplemented, I am a leng. It was Notimplementederror's brother, so I studied it a little bit.
notimplemented hence the name Incredibles. Is "not implemented." Usually used in some algorithms, such as class __eq__,__lt__, note that notimplemented is not an exception, so you cannot
With raise, it should be return notimplemented when not implemented .
We can look at the implementation of the field in Django,
@total_orderingclass field (object): "" " Base class for all Field types" " def __eq__ (self, Other): # Needed For @total_ordering if Isinstance (Other, Field): return self.creation_counter = = Other.creation_counter return notimplemented def __lt__ (self, Other): # needed because Bisect does don't take a comparison function. If Isinstance (Other, Field): return Self.creation_counter < Other.creation_counter return notimplemented
What are the advantages of providing notimplemented? The advantage is that assuming a = = B notimplemented, the __eq__ method of B is called, assuming B does not call the CMP method.
Let's look at the following example:
Class Person: def __init__ (self, Age): Self.age = Age def __eq__ (self, Other): if not isinstance ( Other, person): return notimplemented return self.age = = Other.age
Let's say you have a piece of code in your stability library. And the person may include very many fields. But you want to achieve the comparison between person and integer.
Person=person (ten) print person = = #False
Unfortunately, the result above is false, does not meet our requirements, as to why it is 10, we will say later, we first look at how to solve the problem?
In fact, you can simply encapsulate an age object,
Class Age: def __init__ (self, Age): Self.age = Age def __eq__ (self, Other): return self.age = = Other.age Person=person (age=age) print person = = Age #True
OK, very perfect, so what can we get from the above conclusion?
We are writing some basic code, even if it is not implemented. Also do not raise notimplementederror, but return notimplemented, equivalent to provide to other different objects of the comparative interface, which
Code extensions have great advantages.
Let's take a look at the above so direct and 10 ratio, why is false?
Let's look at the following code:
Class A: def __lt__ (Self, a): return notimplemented def __add__ (Self, a): return Notimplementedprint A () < A () # Trueprint A () < 1 # False
It is very strange, obviously already directly isnotimplemented, why is there a result?
Daring to push the test, the previous description of the final use of CMP, it is very obvious when not defined by the comparison of the ID value. That is, the memory address. After creating the object memory address large, this is the truth.
As for A () < 1, the Python Small integer object was created at initialization time. The memory address must be small. Suppose you don't believe it yet.
Just don't do anything that's not fun.
This is where the brother is puzzled, http://stackoverflow.com/questions/1062096/python-notimplemented-constant.
The beauty of Python [from rookie to master]--notimplemented small analysis