Why a super() statement ?

236 views
Skip to first unread message

srfpala

unread,
Jun 22, 2019, 7:29:07 PM6/22/19
to wxPython-users
In   Precord  text p 15    Show a BitMap uses:

super(MyFrame, self).__init__(parent, id, title, pos, size, style, name)

The above statement seems superfluous, and ASAIK offers nothing new to the App.
Why is it needed ?
Bob

Mike Driscoll

unread,
Jun 22, 2019, 8:14:04 PM6/22/19
to wxPytho...@googlegroups.com
It's not needed. But it doesn't hurt anything either. Also I think it is recommended to use super() that way I'm Python 3.

So while it's not needed it is conventional to use it in the latest version of Python.

Mike

--

Gerhard Schmidt

unread,
Jun 25, 2019, 1:52:39 AM6/25/19
to Mike Driscoll
Hi,

it might not be needed there but it's needed to ensure that all __init__
methods of any superclasses are called.

Not calling super in all __init__ methods might break initialization
when deriving from multiple classes way down the hierarchy.

an example

you have a class A, Class B derived from A and Class C derived from
Class B and a Class D that has nothing to do with class A or B.

The __init__ method of class C call super which calls the B.__init__, in
B.__init__ super is called again and A.__init__ is called. if A.__init__
doesn't call super the execution goes back to B.__init__ and C.__init__
after the methods are finished, but D.__init__ is never called because
the super call in A.__init__ will trigger that.

That's what in experienced in one of my projects I'm migrated from
python 2 to python3. I might be wrong here, but that's what I'm
experiencing.

Before super you called the __init__ method of any class you derived
from directly. Now the super call has to handle that.

Regards
Estartu

Am 23.06.19 um 02:13 schrieb Mike Driscoll:
> --
> You received this message because you are subscribed to the Google
> Groups "wxPython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to wxpython-user...@googlegroups.com
> <mailto:wxpython-user...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/wxpython-users/CAO4gEAsn9%3D1ujzrAcpAUVmWPv_nMrL8HqZH-oNAFKPf9F9mntA%40mail.gmail.com
> <https://groups.google.com/d/msgid/wxpython-users/CAO4gEAsn9%3D1ujzrAcpAUVmWPv_nMrL8HqZH-oNAFKPf9F9mntA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


--
----------------------------------------------------------------------------
Gerhard Schmidt | http://www.augusta.de/~estartu/ |
Fischbachweg 3 | | PGP Public Key
86856 Hiltenfingen | JabberID: est...@augusta.de | auf Anfrage/
Tel: 08232 77 36 4 | | on request
Fax: 08232 77 36 3 | |
estartu.vcf
signature.asc
Reply all
Reply to author
Forward
0 new messages