NotImplimentedError

Ben Finney bignose+hates-spam at benfinney.id.au
Mon Jan 14 23:21:12 EST 2008


George Sakkis <george.sakkis at gmail.com> writes:

> On Jan 14, 5:39 pm, Ben Finney <bignose+hates-s... at benfinney.id.au>
> wrote:
> 
> > I think of NotImplemented as equivalent to None; it's useful as a
> > sentinel value to set an attribute to in (e.g.) an abstract class.
> 
> My guess would be that it is more of an implementation performance
> decision than semantic. Checking 'if result is NotImplemented' is much
> faster than guarding the call with a try/except NotImplementedError
> block

Even better is not to check at all, and just try to use the return
value as normal. If it's the NotImplemented object, exceptions will
soon be raised that explain the problem. EAFP, dotcha know.

> Semantically though, 'return NotImplemented' looks odd compared to
> 'raise NotImplementedError'.

I use 'NotImplemented' as the default value for something that
*shouldn't* be used as-is, because the *programmer* needs to do more
work on the implementation. This either means an abstract base class,
that must be inherited from to override the attribute; or a section of
code that needs more work directly to complete it.

I have never written 'return NotImplemented' except for the
implementation of __cmp__ and friends. I can't think when that would
be a good idea as compared to 'raise NotImplementedError("explanatory
text")' (note: create an instance, don't just raise the class object).

-- 
 \         "To succeed in the world it is not enough to be stupid, you |
  `\                         must also be well-mannered."  -- Voltaire |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list