pure virtual functions in python

Erik Max Francis max at alcyone.com
Sun Jan 12 21:52:44 EST 2003


"Delaney, Timothy" wrote:

> 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 have never used `NotImplementedError` in my python code. Why bother?

I prefer explicit is better than implicit.  Simply using documentation
certainly gets the same effect, I agree, but there's something more
prominently indicating "Hey, you forgot to override something" when you
get a NotImplementedError rather than an AttributeError.  An
AttributeError might just indicate, for instance, that you spelled the
method name wrong.  A NotImplementedError means that I spelled it right,
but someone, somewhere along the line forgot to override a method which
required overriding.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ If the sky should fall, hold up your hands.
\__/ (a Spanish proverb)
    Maths reference / http://www.alcyone.com/max/reference/maths/
 A mathematics reference.




More information about the Python-list mailing list