NotImplimentedError

Duncan Booth duncan.booth at invalid.invalid
Mon Jan 14 05:39:26 EST 2008


Gary Herron <gherron at 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.



More information about the Python-list mailing list