A question about Cody Precord's Cookbook

352 views
Skip to first unread message

Boštjan Mejak

unread,
Feb 17, 2011, 6:26:11 PM2/17/11
to wxPython Mailing List
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.

Robin Dunn

unread,
Feb 18, 2011, 12:34:23 PM2/18/11
to wxpytho...@googlegroups.com

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

Cody Precord

unread,
Feb 18, 2011, 12:59:51 PM2/18/11
to wxpytho...@googlegroups.com
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

Boštjan Mejak

unread,
Feb 18, 2011, 5:38:18 PM2/18/11
to wxpytho...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages