A question about Cody Precord's Cookbook

Showing 1-4 of 4 messages
A question about Cody Precord's Cookbook Boštjan Mejak 2/17/11 3:26 PM
Hello, this question is only intended for you, Cody. Why do you fancy the super() method in the class' __init__ for the examples in your book? Isn't, for example, wx.Frame.__init__(...) good enough? Please explain.
Re: [wxPython-users] A question about Cody Precord's Cookbook Robin Dunn 2/18/11 9:34 AM

In most cases there isn't any difference.  In cases where you have
multiple inheritance, the super() helps to ensure that the proper
method resolution order (MRO) is followed when moving up the inheritance
tree.

http://stackoverflow.com/questions/576169/understanding-python-super

--
Robin Dunn
Software Craftsman
http://wxPython.org

Re: [wxPython-users] A question about Cody Precord's Cookbook Cody Precord 2/18/11 9:59 AM
Hi,

On Fri, Feb 18, 2011 at 11:34 AM, Robin Dunn <ro...@alldunn.com> wrote:
> On 2/17/11 3:26 PM, Boštjan Mejak wrote:
>>
>> Hello, this question is only intended for you, Cody. Why do you fancy
>> the super() method in the class' __init__ for the examples in your book?
>> Isn't, for example, wx.Frame.__init__(...) good enough? Please explain.
>
> In most cases there isn't any difference.  In cases where you have multiple
> inheritance, the super() helps to ensure that the proper method resolution
> order (MRO) is followed when moving up the inheritance tree.
>
> http://stackoverflow.com/questions/576169/understanding-python-super
>

It also makes it easier to do re-factoring.

For example:

class Foo(wx.Frame):
    def __init__(...):
        wx.Frame.__init__(...)

         # do custom stuff...

    def SetSize(...):
       # Do custom stuff....
        wx.Frame.SetSize(...)

Now say later on I need to change the Foo class to inherit from
another intermediate class 'Bar' that also overrides the __init__ and
SetSize methods of the base wx.Frame class.

i.e) class Foo(Bar):

Since I didn't use super above I would have to go through the whole
class and update all the places that are making explicit calls to the
Frame super class otherwise the methods in the intermediate Bar class
would never get invoked. If instead I had used super everywhere I
would only need to make the one change of 'wx.Frame -> Bar' in the
class declaration as shown above.


Cody

Re: [wxPython-users] A question about Cody Precord's Cookbook Boštjan Mejak 2/18/11 2:38 PM
Taken from http://stackoverflow.com/questions/576169/understanding-python-super"Note that the syntax changed in Python 3.0: you can just say super().__init__() instead ofsuper(ChildB, self).__init__() which IMO is quite a bit nicer."

When wxPython is going to support Python 3.x, then I'm gonna use super(). 'Till then I'm on wx.ClassName.__init__() side.