callable virtual method

Jean-Michel Pichavant jeanmichel at sequans.com
Fri Aug 14 10:49:47 EDT 2009


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."""
        _log.info('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)

JM



More information about the Python-list mailing list