Python super()- Funktion
Beispiel
Erstellen Sie eine Klasse, die alle Methoden und Eigenschaften von einer anderen Klasse erbt:
class Parent:
def __init__(self, txt):
self.message = txt
def printmessage(self):
print(self.message)
class Child(Parent):
def __init__(self,
txt):
super().__init__(txt)
x = Child("Hello,
and welcome!")
x.printmessage()
Definition und Verwendung
Die super()
Funktion wird verwendet, um Zugriff auf Methoden und Eigenschaften einer Eltern- oder Geschwisterklasse zu gewähren.
Die super()
Funktion gibt ein Objekt zurück, das die übergeordnete Klasse darstellt.
Syntax
super()
Parameterwerte
Keine Parameter