The beauty of Python [from rookie to master]--notimplemented small analysis

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.