Python NotImplemented 常量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1062096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python NotImplemented constant
提问by Anurag Uniyal
Looking through decimal.py, it uses NotImplementedin many special methods. e.g.
翻看decimal.py,它用NotImplemented在许多特殊的方法中。例如
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.
未实现
可以通过“丰富比较”特殊方法(
__eq__()、__lt__()、 和朋友)返回的特殊值,以指示未针对其他类型进行比较。
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.
它似乎是一个魔术对象,如果从其他特殊方法返回,则会引发TypeError,而在“丰富的比较”中,特殊方法什么也不做。
e.g.
例如
print A() < A()
prints True, but
打印True,但是
print A() + 1
raises TypeError, so I am curious as to what's going on and what is the usage/behavior of NotImplemented.
raises TypeError,所以我很好奇发生了什么以及 NotImplemented 的用法/行为是什么。
回答by Brandon E Taylor
NotImplementedallows 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).
NotImplemented允许您指示尚未实现两个给定操作数之间的比较(而不是指示比较有效,但False对两个操作数产生 )。
From the Python Language Reference:
来自Python 语言参考:
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.
对于对象 x 和 y,首先
x.__op__(y)尝试。如果这未实现或返回 NotImplemented,y.__rop__(x)则尝试。如果这也未实现或返回 NotImplemented,则会引发 TypeError 异常。但看到以下异常:上一项的例外:如果左操作数是内置类型或新式类的实例,而右操作数是该类型或类的适当子类的实例并覆盖基类的
__rop__()方法,则右操作数操作的__rop__()方法是左操作的尝试过__op__()的方法。这样做是为了让子类可以完全覆盖二元运算符。否则,左操作数的__op__()方法将始终接受右操作数:当需要给定类的实例时,该类的子类的实例总是可接受的。
回答by Anurag Uniyal
It actually has the same meaning when returned from __add__as from __lt__, the difference is Python 2.x is trying other ways of comparing the objects before giving up. Python 3.x does raise a TypeError. In fact, Python can try other things for __add__as well, look at __radd__and (though I'm fuzzy on it) __coerce__.
当从__add__from返回时它实际上具有相同的含义__lt__,不同之处在于 Python 2.x 在放弃之前尝试其他比较对象的方法。Python 3.x 确实会引发 TypeError。事实上,Python 也可以尝试其他的东西__add__,看看__radd__和(虽然我对它很模糊)__coerce__。
# 2.6
>>> class A(object):
... def __lt__(self, other):
... return NotImplemented
>>> A() < A()
True
# 3.1
>>> class A(object):
... def __lt__(self, other):
... return NotImplemented
>>> A() < A()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: A() < A()
See Ordering Comparisions (3.0 docs)for more info.
有关更多信息,请参阅订购比较(3.0 文档)。
回答by Lennart Regebro
If you return it from __add__it will behave like the object has no __add__method, and raise a TypeError.
如果您从中返回它,__add__它将表现得像对象没有__add__方法一样,并引发一个TypeError.
If you return NotImplementedfrom a rich comparison function, Python will behave like the method wasn't implemented, that is, it will defer to using __cmp__.
如果NotImplemented从富比较函数返回,Python 将表现得好像该方法未实现,也就是说,它将推迟使用__cmp__.

