callable virtual method

MRAB python at
Fri Aug 14 17:49:52 CEST 2009

Jean-Michel Pichavant wrote:
> Hi fellows,
> Does anyone know a way to write virtual methods (in one virtual class) 
> that will raise an exception only if called without being overridden ?
> Currently in the virtual method I'm checking that the class of the 
> instance calling the method has defined that method as well.
> Example:
> class Stream(object):
>    """Interface of all stream objects"""
>    def resetStats(self):
>        """Reset the stream statistics. All values a zeroed except the 
> date."""
>'Reset statistics of %s' % self)
>        if self.__class__.resetStats == Stream.resetStats:
>            raise NotImplementedError()
> It works but it's tedious, I have to add these 2 lines to every virtual 
> method, changing the content of the 2 lines.
> Maybe there is a nice/builtin way to do so (python 2.4)
Why are you checking which class it's in? The method in the base class
will be called only if it hasn't been overridden in the subclass.

More information about the Python-list mailing list