Tag Archives: # [Python Development]

[Python] notimplemented and notimplemented error

NotImplemented is a non-exception object, and NotImplementedError is an exception object.

>>> NotImplemented
NotImplemented
>>> NotImplementedError
<type 'exceptions.NotImplementedError'>


>>> type(NotImplemented)
<type 'NotImplementedType'>
>>> type(NotImplementedError)
<type 'type'>

TypeError is obtained if NotImplemented is thrown, because it is not an exception. Throwing a NotImplementedError will normally catch the exception.

>>> raise NotImplemented

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    raise NotImplemented
TypeError: exceptions must be old-style classes or derived from BaseException, not NotImplementedType


>>> raise NotImplementedError

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    raise NotImplementedError
NotImplementedError

Why is there a NotImplemented and a NotImplementedError?
Sorting a list in Python often USES a comparison operation such as ___ ().
Sometimes Python’s internal algorithms choose another way to determine the comparison result, or simply choose a default result. If an exception is thrown, the sorting operation will be broken. Therefore, it will not be thrown if NotImplemented is used, thus Python can try other methods.
The NotImplemented object sends a signal to the runtime environment to tell it that if the current operation fails, it should check out other feasible methods. For example, in the expression a == b, if a. ___, eq__(b) returns NotImplemented, Python will try B. ___ (a). If the call to the method of B/eq__ can return True or False, the expression is successful. If b.__eq__(a) also fails, Python continues to try other methods, such as using! = to compare.

ref:http://www.cnblogs.com/ifantastic/p/3682268.html