pure virtual functions in python

Peter Hansen peter at engcorp.com
Sun Jan 12 21:22:33 EST 2003


"Delaney, Timothy" wrote:
> 
> > From: polux [mailto:polux2001 at wanadoo.fr]
> >
> > how to emulate them ?
> > I mean, how to force a user to redifine some functions in a derivated
> > class ?
> 
> Most responses have used `NotImplementedError`. This is fine, and useful if
> you want the abstract class to document what methods it defines.
> 
> The other approach is to document what attributes/methods other functions
> require their parameters to have. This achieves the same effect (but throws
> an `AttributeError` rather than a `NotImplementedError`), but is in many
> ways more versatile.

I'm not sure I understand how this would force a subclass to implement
a given method (which is how I understand the phrase "achieves the same
effect").

> I have never used `NotImplementedError` in my python code. Why bother?

If nothing else, because it's more self-documenting... AttributeError
is a relatively common mistake from typos and other bugs.  NotImplementedError
is pretty much unique to situations like the OP describes.

-Peter




More information about the Python-list mailing list