Can a base class know if a method has been overridden?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Mon Sep 24 12:02:36 EDT 2007


On Mon, 24 Sep 2007 15:48:07 +0000, Ratko wrote:

> I have a base class EvtHandler that has methods defined to handle
> certain events. You then subclass from EvtHandler and override the
> methods for the events you want to receive. If a method has been
> overridden, the base class will automatically register for those
> events to make sure that they are even delivered to this handler
> (which is why I would need to know whether a method has been
> overridden or not). Of course, there are other ways of doing this
> which would require a bit more work from the subclass... I just
> thought this would be a neat "automatic" way of registering for
> events.
> 
> For example:
> 
> class EvtHandler:
>     def __init__(self):
>         if onKey is overridden:
>              register_for_key_events()
> 
>     def onKey(self):
>         pass
> 
> 
> class MyHandler(EvtHandler):
>     def onKey(self):
>         # do something here....

Maybe "tagging" the original `on_key()`:

class EvtHandler:
    def __init__(self):
        if not hasattr(self.on_key, 'dummy'):
             print 'register_for_key_events()'

    def _dummy_handler(self):
        pass
    _dummy_handler.dummy = True
    
    on_key = _dummy_handler
    on_whatever = _dummy_handler


class MyHandler(EvtHandler):
    def on_key(self):
        print 'Do something...'

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list