Extending Methods Vs Delegates

Sybren Stuvel sybrenUSE at YOURthirdtower.com.imagination
Sun Mar 26 06:18:28 EST 2006

vbgunz enlightened us with:
> I hope I've made some sense with this question. I ultimately wish to
> know just one real thing. Regardless of the name of the second
> example above, what is the purpose of calling a sub class method
> from a super class instance? What is the application to such a
> design?

I've seen this style of programming before, in wxWidgets. The
constructor of a class does various things. This is roughly what

class wxSomeClass(wxParent):
    def __init__(self, *args, **kwargs):
        # Do various things

        if not self.OnInit():
            raise RuntimeError("Initialization failed")

        # Do other things that need to be done after custom
        # initialization

    def OnInit(self):
        return True

This makes it easier to simply provide custom initialization, without
having to redo everything in __init__. Calling the superclass'
function is fine if you want to add behaviour before and/or after
calling it. This example however lets you customize behaviour that's
put in the middle of the __init__ function.

> Maybe an example will help?

I hope so :)

The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself? 
                                             Frank Zappa

More information about the Python-list mailing list