What is NotImplemented ?

Michael Hudson mwh at python.net
Fri Apr 26 10:40:59 EDT 2002


Alexandre Fayolle <alf at logilab.fr> writes:

> A quick question, here. What is the built in NotImplemented object? 
> And what can you do with it? I thought I could raise it as an 
> exception, but this is not the case (I use NotImplementedError instead).

I think you can return it from implementations of, say, __add__ to
mean roughly "I don't know what to do here".

Eg:

>>> class Num: 
...  def __add__(self, other):
...   return NotImplemented
...  def __radd__(self, other):
...   print "radd!"
...   return 1
... 
>>> Num() + Num()
radd!
1

Returning NotImplemented from the __add__ makes Python go off and call
the __radd__ method.

HTH,
M.

-- 
  ... but I guess there are some things that are so gross you just have
  to forget, or it'll destroy something within you.  perl is the first
  such thing I have known.              -- Erik Naggum, comp.lang.lisp



More information about the Python-list mailing list