Newbie Question: Abstract Class in Python

Erik Max Francis max at alcyone.com
Sun Jul 6 20:40:51 EDT 2003


Kevin Bass wrote:

> I am new to Python and want to know how to implement an abstract
> class? I
> have read through different books and I have no found this
> information.
> Thanks!

There's no builtin way to do that in Python.  Typically if I'm doing
something where I want to make a class that yells at me if I 1.
accidentally instantiate it directly or 2. fail to override one of its
methods in a subclass, I do something like:

	class AbstractSomething:
	    def __init__(self):
	        if self.__class__ is AbstractSomething:
	            raise NotImplementedError

	    def aMethod(self, ...):
	        raise NotImplementedError

	    def anotherMethod(self, ...):
	        raise NotImplementedError

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ They love too much that die for love.
\__/  (an English proverb)




More information about the Python-list mailing list