Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the NotImplemented type built into Python programming

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to use the built-in NotImplemented type in Python programming". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

What is it, NotImplemented? > type (NotImplemented)

NotImplemented is one of six constants of Python in the built-in namespace. Others are False, True, None, Ellipsis and debug. Much like Ellipsis, [NotImplemented] can be reassigned (overridden). Assigning a value to it, or even changing the property name, does not produce a 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 > NotImplementedNotImplemented > NotImplemented =' do not' > NotImplemented'do not' II, what is the use of it? When do you use it?

NotImplemented is a special value that can be returned by binary special methods (such as _ _ eq__ (), lt (), add (), rsub (), etc.), indicating that a type does not perform these operations as other types do. Similarly, it may be returned by binary special methods that in place (for example, _ _ imul__ (), iand (), and so on).

Also, its actual value is True:

> bool (NotImplemented) True

You might ask yourself, "but I think I should generate a NotImpementedError when this operation is not implemented." We will look at some examples of why this is not the case when implementing binary special methods.

Let's look at the use of the NotImplemented constant, using _ _ eq__ () to encode two very basic (and useless) classes An and B. For this simple example, to avoid interference, _ _ ne__ () will not be implemented, but in general, every time you implement _ _ eq__ (), ne () should also be 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 an A with an A') return other.value = = self.value if isinstance (other) B): print ('Comparing an A with a B') return other.value = = self.value print (' Could not compare A with 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 () and see what happens.

As a reminder, in Python, a = = b calls a.eq (b):

> A1 = = a1Comparing an A with an ATrue

As expected, A1 equals A1 (oneself), and use _ _ eq__ () in class A to make this comparison.

Comparing b1 with itself produces a similar result:

> > b1 = = b1Comparing a B with another BTrue

Now, what if we compare A1 and b1? Since _ _ eq__ () in A checks whether other is an instance of B, we want a1.eq (b1) to handle the comparison and return True:

> A1 = = b1Comparing an A with a BTrue

okay. Now, if we compare b1 with A1 (that is, call b1.eq (A1)), we want to return NotImplemented. This is because the _ _ eq__ () of B is only compared with other instances of B.

Let's see what happened:

> > b1 = = a1Could not compare B against the other classComparing an A with a BTrue

clever! The b1.eq (A1) method returns NotImplemented, which results in a call to the _ _ eq__ () method in A. And because the _ _ eq__ () in A defines the comparison between An and B, the correct result (True) is obtained.

This is what the returned NotImplemented does. 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 indicates that Python is trying to use a1.eq (b1). Because A1 is sufficient to return True, this expression can be successful. If _ _ eq__ () in An also returns NotImplemented, the runtime degenerates to using the built-in comparison behavior, which is the identifier of the comparison object (in CPython, the address of the object in memory).

Note: if NotImpementedError is thrown when b1.eq (A1) is called without processing, code execution will be interrupted. NotImplemented cannot be thrown, only to further test whether there are other methods available to call.

This is the end of the introduction to "how to use NotImplemented types built into Python programming". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report