newbie-question: abstract classes
Erik Max Francis
max at alcyone.com
Sat Sep 14 18:16:16 EDT 2002
"T. Kaufmann" wrote:
> I want to create an abstract class but it doesn't work.
> What' s wrong here?
> class Abstract:
> def __init__(self): return
As others have pointed out, it's strange to have an explicit return
statement in .__init__. Better would be to use pass when the .__init__
method is truly empty; otherwise it's not necessary.
> class Concrete:
You don't say how it doesn't work, but one obvious issue here is that
Concrete doesn't derive from Abstract. This should read:
An idiom I use for abstract classes is to have its .__init__ method
check to make sure that it truly is something other than the base class.
if self.__class__ is Abstract:
def method(self): raise NotImplementedError
def method(self): print "in Concrete.method"
Now even trying to create an instance of the Abstract class will result
in an exception.
Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/ \ A man that studieth revenge keeps his own wounds green.
\__/ Francis Bacon
Sade Deluxe / http://www.sadedeluxe.com/
The ultimate Sade encyclopedia.
More information about the Python-list