[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: NotImplimentedError

Gary Herron

1/14/2008 8:45:00 AM

hakim ouaras wrote:
> Hi,
>
> I am begining with python, I want to know what is the utility and how
> to use the expression "NotImplementedError".
>
> Thak you for your answers
> Hakim
>
> ------------------------------------------------------------------------
> Never miss a thing. Make Yahoo your homepage.
> <http://us.rd.yahoo.com/...*http://www.yahoo.co...

It's meant to be used to mark a procedure that you intend to write, but
have not yet done so.

The procedure you have not yet written "raises" that exception to
indicate that it is not yet implemented and should not be called:

def DoSomething(some, args):
""" This procedure will do ... great things someday. """
raise NotImplementedError


Then *if* the writer of the application that will call it forgets that's
it not yet implemented, and mistakenly tries to call it, an error will
be raised.

It's not so useful in a small application, or a single person project,
but it does become useful if several people are writing different parts
(say a library and an application) at the same time.

Gary Herron

6 Answers

Duncan Booth

1/14/2008 10:39:00 AM

0

Gary Herron <gherron@islandtraining.com> wrote:

> hakim ouaras wrote:
>> Hi,
>>
>> I am begining with python, I want to know what is the utility and how
>> to use the expression "NotImplementedError".
>>
>> Thak you for your answers
>> Hakim
>>
>
> It's meant to be used to mark a procedure that you intend to write,
> but have not yet done so.

More often it is used to mark a method which is some part of a class
interface but is not implemented in that specific class.

Typically you do this when writing an abstract base class: whoever uses
it must implement particular methods, and if they forget then you raise
NotImplementedError.

For examples just grep the Python lib sources. e.g. optparse has
an abstract class HelpFormatter which has a couple of methods that must
be implemented in subclasses:

class HelpFormatter:
....
def format_usage(self, usage):
raise NotImplementedError, "subclasses must implement"

def format_heading(self, heading):
raise NotImplementedError, "subclasses must implement"
....

class IndentedHelpFormatter (HelpFormatter):
"""Format help with indented section bodies.
"""
....
def format_usage(self, usage):
return _("Usage: %s\n") % usage

def format_heading(self, heading):
return "%*s%s:\n" % (self.current_indent, "", heading)

and so on.

George Sakkis

1/14/2008 2:02:00 PM

0

By the way, why do we need both NotImplementedError and the
NotImplemented singleton object ? Couldn't we have just one of them ?

Neil Cerutti

1/14/2008 2:32:00 PM

0

On Jan 14, 2008 9:01 AM, George Sakkis <george.sakkis@gmail.com> wrote:
> By the way, why do we need both NotImplementedError and the
> NotImplemented singleton object ? Couldn't we have just one of them ?

I think we need both because an unimplemented method is an error,
while an unimplemented rich comparison between disparate types is
(usually) not. It could be made to work with one object fulfilling
both functions, but then the name would be wrong for one case or the
other.

--
Neil Cerutti <mr.cerutti+python@gmail.com>

Ben Finney

1/14/2008 11:35:00 PM

0

Ben Finney <bignose+hates-spam@benfinney.id.au> writes:

> I think of NotImplemented as equivalent to None

Not "equivalent"; rather "comparable in usage".

--
\ "[W]e are still the first generation of users, and for all that |
`\ we may have invented the net, we still don't really get it." |
_o__) -- Douglas Adams |
Ben Finney

George Sakkis

1/15/2008 2:03:00 AM

0

On Jan 14, 5:39 pm, Ben Finney <bignose+hates-s...@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, and since the motivation for NotImplemented was AFAIK rich
comparisons, the potential overhead could be substantial (e.g. a tight
'while i<j' loop that's executed millions of times). Semantically
though, 'return NotImplemented' looks odd compared to 'raise
NotImplementedError'.

George

Ben Finney

1/15/2008 4:21:00 AM

0

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

> On Jan 14, 5:39 pm, Ben Finney <bignose+hates-s...@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