These are some pretty basic OO methods in Python. Read here.
super
and self
are similar:
super() lets you avoid referring to the base class explicitly, which
can be nice. But the main advantage comes with multiple inheritance,
where all sorts of fun stuff can happen. See the standard docs on
super if you haven't already.
(from this answer)
Here's an example of super
in action:
class Animal(object):
def __init__(self, speed, is_mammal):
self.speed = speed
self.is_mammal = is_mammal
class Cat(Animal):
def __init__(self, is_hungry):
super().__init__(10, True)
self.is_hungry = is_hungry
barry = Cat(True)
print(f"speed: {barry.speed}")
print(f"is a mammal: {barry.is_mammal}")
print(f"feed the cat?: {barry.is_hungry}")
You can see that super
is calling the base class (the class that the current class inherits), followed by an access modifier, accessing the base class' .__init__()
method. It's like self
, but for the base class.