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
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
super().__init__()
instead ofsuper(ChildB, self).__init__()
which IMO is quite a bit nicer."