Tuesday, June 14, 2022

Python - super()

 The Python super() method lets you access methods from a parent class from within a child class. This helps reduce repetition in your code. 

One core feature of object-oriented programming languages like Python is inheritance. Inheritance is when a new class uses code from another class to create the new class.

When you’re inheriting classes, you may want to gain access to methods from a parent class. That’s where the super() function comes in.

Here is the syntax for the super() method:

class Food():
	def __init__(self, name):
		self.name = name
class Cheese(Food):
	def __init__(self, brand):
		super().__init__()
		self.brand = brand

The super() method does not accept any arguments. 

Whatever comes last in Third() is the parent class. so when calling Third(), it will call Second() first, then First().






No comments:

Post a Comment