[Tutor] super() vs. explicitly calling base class?
Cameron Simpson
cs at cskk.id.au
Sat Sep 21 04:19:12 EDT 2019
On 20Sep2019 23:47, boB Stepp <robertvstepp at gmail.com> wrote:
>Assumption: I have only TWO classes (Not concerned about 3+ class
>inheritance scenario), Base and Child. Is there any advantage to
>using super() over explicitly calling Base from within Child? My
>following trivial example does not suggest any difference:
>
>>>> class Base:
> def __init__(self):
> print("This is the Base class!")
>>>> class Child(Base):
> def __init__(self):
> print("This is the Child class!")
> def call_base(self):
> print("About to call the Base class with super!")
> super().__init__()
> print("Now explicitly calling the Base class!")
> Base.__init__(self)
[...]
Well you wouldn't do both of course.
If you have the setup above, then super() is equivalent to directly
calling the base class.
Personally I tend to use super() so that in the future when I make the
class more complex (or just change the superclass from Base to
OtherBase) it reduces the number of things needing modification.
Cheers,
Cameron Simpson <cs at cskk.id.au>
More information about the Tutor
mailing list