Abstract class

Fredrik Lundh fredrik at pythonware.com
Sun Sep 14 15:37:16 EDT 2008


Mr.SpOOn wrote:

> Well, I know the difference between an abstract class and an inherited
> one. The idea was to create a main class Note, with abstract methods,
> and implement these methods in the other classes.

The pragmatic way to implement abstract classes in Python is to define 
an ordinary base class, and either just leave out the methods that must 
be defined in subclasses, or provide stub methods that simply raise a 
NotImplementedError exception:

     class AbstractThing(object):
         ...
         def some_method(self, args):
             raise NotImplementedError

There are more "clever" ways to do things, but they're usually overkill.

 > Anyway, I think I need an abstract class. Or not?

Depends on how much generally useful functionality you can come up with 
for the base class.  It's not like Python prevents you from overriding a 
non-abstract method, after all.

I suggest you skip further discussion and get to work.  I'm sure you'll 
figure out what works best for your specific case once you've written a 
few classes.

</F>




More information about the Python-list mailing list