What is proper way to require a method to be overridden?

belinda thom bthom at cs.hmc.edu
Fri Jan 5 00:57:22 EST 2007


On Jan 4, 2007, at 7:56 PM, Thomas Ploch wrote:

> Gabriel Genellina schrieb:
>> At Thursday 4/1/2007 23:52, jeremito wrote:
>>
>>> I am writing a class that is intended to be subclassed.  What is the
>>> proper way to indicate that a sub class must override a method?
>>
>> If any subclass *must* override a method, raise  
>> NotImplementedError in
>> the base class (apart from documenting how your class is supposed  
>> to be
>> used).
>>
>>
>
> I learn so much from this list. I didn't even know this error existed.

Me too.

I was looking for a definitive list of Python errors, although I  
realize some folks do things like:

class foo :
    def __init__() :
       <whatever>

    def bar() :
       abstract

class baz(foo) :
    def __init__() :
       <whatever>

    def bar() :
       <shomething useful>

This does what you want in that if baz's bar wasn't defined, calling  
this method on a baz instance would give some kind of "variable not  
found error."

I imagine the NotImplementedError is actually a defined object, but I  
really don't know.

So, back to my question: is a catalog of standard python errors  
available? I've looked on the python site but had no success.

Thanks,

--b




More information about the Python-list mailing list