#56 Python Tutorial for Beginners | Constructor in Inheritance
Summary
TLDRIn this video, Ivan 20 explains key concepts of inheritance in Python, focusing on constructors and method resolution order (MRO). The tutorial covers how constructors behave when inheriting from one or multiple classes, and how Python determines which constructor to call. The 'super()' method is introduced, demonstrating how it helps call parent class methods, even in cases of multiple inheritance. Ivan also highlights how MRO works, ensuring Python resolves methods from left to right when dealing with multiple inherited classes. This tutorial deepens the understanding of object-oriented programming in Python.
Takeaways
- 👨💻 Inheritance allows one class (B) to inherit features from another class (A), enabling code reuse.
- 🧩 Multiple inheritance occurs when a class inherits from two or more classes, such as Class C inheriting from both Class A and Class B.
- 🏗️ Constructors in inheritance: Creating an object of class B can call the constructor of class A even if B has its own constructor.
- 🔄 If both Class A and B have their own constructors, B's constructor will be called when an object of B is created, unless explicitly specified otherwise.
- ⚙️ The `super()` method is used to call the parent class's constructor or other methods within a subclass.
- 📜 If Class C inherits from both A and B, the `super()` method will call the constructor of the class listed first in the inheritance chain (left to right).
- 📐 Method Resolution Order (MRO) determines the sequence in which inherited methods are called in a class hierarchy.
- ⚡ MRO always follows a left-to-right approach when dealing with multiple inheritance scenarios.
- 📝 The `super()` method can also be used to call any other methods from the parent class, not just constructors.
- 🔄 If two classes have methods with the same name, the method from the class on the left side of the inheritance hierarchy will be called first.
Q & A
What is inheritance in Python?
-Inheritance in Python allows a class (subclass) to inherit methods and properties from another class (superclass). This helps in reusing the features of an existing class without rewriting code.
How does multiple inheritance work in Python?
-Multiple inheritance occurs when a class inherits from more than one parent class. For instance, Class C can inherit features from both Class A and Class B. This allows Class C to access the methods and properties of both classes.
What happens if both parent and child classes have their own constructors?
-If both the parent and child classes define their own `__init__` constructors, the child class's constructor will override the parent's constructor by default. To explicitly call the parent's constructor, the `super()` function is used.
What is the purpose of the `super()` function in inheritance?
-The `super()` function allows the child class to call methods from its parent class. It is commonly used to call the parent's `__init__` method within the child class, ensuring both the child and parent constructors are executed.
What is Method Resolution Order (MRO) in Python?
-MRO defines the order in which base classes are searched when looking for a method or attribute. In multiple inheritance, Python looks for methods from left to right in the class definition. This ensures a predictable order of inheritance.
What happens if multiple classes in an inheritance hierarchy have methods with the same name?
-If multiple parent classes have methods with the same name, Python will follow the MRO and execute the method from the first class in the hierarchy. For example, if Class C inherits from Class A and Class B, it will use the method from Class A first.
How does constructor inheritance work in Python?
-When an object is created from a child class that does not have its own constructor, the constructor of the parent class is automatically called. If the child class has its own constructor, the parent's constructor can still be called using `super()`.
Can `super()` be used to call methods other than constructors?
-Yes, `super()` can be used to call any method from a parent class, not just the constructor. This allows a child class to reuse methods from the parent class without directly accessing them.
What is the default behavior when creating an object in a class with multiple inheritance?
-When creating an object in a class with multiple inheritance, the constructor of the first parent class (based on the MRO) is called by default unless `super()` is used to call constructors of other parent classes.
How can you manually check the Method Resolution Order (MRO) of a class?
-You can check the Method Resolution Order (MRO) of a class using the `mro()` method. For example, `ClassC.mro()` will return a list of classes in the order they will be searched for methods.
Outlines
👨💻 Understanding Inheritance in Python Classes
This section introduces the concept of inheritance in Python, explaining how one class can inherit features from another. It covers single inheritance (Class B inherits from Class A) and multiple inheritance (Class C inherits from both A and B). The narrator highlights how inheritance allows a subclass to access features of its superclass, but the reverse is not possible. The paragraph sets up the discussion for constructors and method resolution order (MRO), which will be explored further in the video.
🚀 Constructors in Inheritance: How They Work
This paragraph explains how constructors behave in inheritance. It shows that when an object of a subclass (B) is created, the constructor of the superclass (A) is automatically called if the subclass does not define its own constructor. The narrator demonstrates this by creating objects of classes A and B, and explains the behavior when each class has its own constructor. The use of the `super()` method to explicitly call the constructor of the superclass is introduced as a way to control the execution order.
🧠 Using the `super()` Method in Python
The focus here is on the `super()` method, which is used to access the features of a superclass in Python. The narrator demonstrates how `super()` works by showing how it calls the constructor of Class A from Class B. The paragraph explores the default behavior of constructors and explains that by using `super()`, you can explicitly call both the subclass's and the superclass's constructor, ensuring that the superclass's initialization code is also executed.
🔄 Method Resolution Order (MRO) in Multiple Inheritance
This paragraph introduces the concept of Method Resolution Order (MRO), explaining how Python resolves method calls in the case of multiple inheritance. The narrator sets up a scenario where Class C inherits from both Class A and Class B. MRO ensures that methods are called from left to right, favoring the first superclass listed. The narrator demonstrates this behavior by calling a method shared by both A and B, and showing that the method from A is called first due to MRO.
🎯 Practical Example of MRO in Action
Building on the previous discussion of MRO, this paragraph provides a practical example. The narrator demonstrates how the same method name in both Class A and Class B will always call the method from Class A if Class C inherits from A and B, as per the left-to-right rule of MRO. The example reinforces the importance of understanding MRO when working with multiple inheritance in Python.
📚 Applying the `super()` Method Beyond Constructors
In this final section, the narrator explains how the `super()` method can be used to call not just constructors but also other methods from a superclass. A demonstration is provided, showing how to use `super()` to call a method from the superclass even when the method is redefined in the subclass. This reinforces the flexibility of the `super()` method and concludes the discussion on inheritance, constructors, and method resolution order in Python.
Mindmap
Keywords
💡Inheritance
💡Superclass
💡Subclass
💡Constructor
💡Method Resolution Order (MRO)
💡Multiple Inheritance
💡Super
💡Feature
💡Parent Class
💡Object
Highlights
Introduction to Python inheritance and multiple inheritance, discussing the concept of one class inheriting features from one or more classes.
Explanation of constructors in inheritance, focusing on how the constructor of the parent class is called when creating an object of the child class.
Demonstration of how to define a custom constructor using the `__init__` method in Python.
Clarification that a subclass can access all features of the superclass, but the reverse is not true (i.e., a superclass cannot access features of a subclass).
Explanation of what happens when an object of the subclass is created without defining its own constructor, in which case it calls the constructor of the superclass.
Introduction to the `super()` method, which allows a subclass to call the constructor of its parent class in Python.
Practical demonstration of using `super()` to call the parent class’s constructor from the subclass’s constructor.
Explanation of method resolution order (MRO) in Python, where classes are checked in left-to-right order when dealing with multiple inheritance.
Clarification that in the case of multiple inheritance, Python will first look for methods in the current class and then proceed to parent classes from left to right.
Demonstration of how Python's MRO prioritizes the first class listed in the inheritance hierarchy during method resolution.
Example showing that if two parent classes have the same method name, the method from the left-most parent class is called due to Python’s MRO.
Discussion of how the `super()` method can be used not only to call constructors but also to call other methods from the superclass.
Clarification that `super()` provides a way to represent and call methods from the superclass, ensuring the child class can access features from its parent class.
Demonstration of calling a specific method from a superclass using `super()` in Python.
Conclusion and invitation for viewers to continue engaging with the content through comments and subscriptions.
Transcripts
[Music]
welcome back aliens my name is Ivan 20
and let's continue the series on Python
now to this fine we have talked about
oops concept right and which we have
seen inheritance right now what is
inheritance if you already have some
classes and if you want to use the
existing features you will simply say
class B which will take the features
from a that so class b inherits a or you
can go with a concept of multiple
inheritance where one class will extend
from two different classes all more than
two classes right so in this case we
have Class A Class B and then we have
plus C which is 20 which has a feature 5
but it is also inheriting a and B so we
can say C has five features right so
that makes sense now what do I do in
this video is we'll talk about two
topics the first one is the constructor
in inheritance how it behaves and we
will also talk about method resolution
order all you can say mr o so let's
start the constructor time bin let's
ignore C ok so let's say we don't have
this C something note that part and in
fact will delete that part from for time
bin and this focus only on a and B where
the B is inheriting a ok so in total in
B we have 4 features right now what
happens is if I create an object of a so
let's say if I say a1 is equal to
constructor of a so this will work right
so when you say a bracket that's a
constructor it will call a method which
is in it now even if you don't define it
it is there somewhere right but let me
define my own constructor or in it here
so I will say def and will define the
heat method and this in it meter will
print one stuff so let me say print in a
in it nothing much we are not doing
anything extra here we are just printing
in a in it that's it now what do you
think what it will print of course we
are creating object of a right so it
will only execute a C with the object of
a you cannot access features of B ok so
even if I try example if I say a1 dot
you can only access feature 1 and
feature 2 but yes if you have an object
of B you can access all the features
right or features from a and B because b
is a subclass and a is superclass and as
I mentioned subclass can access all the
features from superclass but that's not
wise versa right a superclass cannot
access
the visas of subclass not even one which
are subclass okay this is fun
now what if I create object of B so I'm
not going to object of a here putting on
stuff P doesn't matter what is a 1 or B
1 important is if I create an object B
will call the constructor that's the
question because we are not getting the
object of a we are creating almost of B
will it Paul the constructor of a that's
the question let me run this code and it
works ok so even if you have the object
of B it will still call the constructor
of a right that's how it works but what
if you have your own constructor what if
you have your own init method example in
be seen B we don't have that in it right
and that's what it's going up so let me
repeat since we don't have in it inside
B that's why it is going to a but what
if you already have in it with you so I
will simply copy paste here in B so both
have in it but then in this I'll print
in B in it right so in a via printing in
a in it and B we are printing in be in
it so this makes sense now wafer than
this code what it will print it will it
print in a in it or in B in it
of course right if you are cutting of it
of B it should only call the init of P
right and let's see if that works and
the test on this code and if we alright
so when you run the code you can see we
only got in be in it so that means if
you create an object of B first it will
try to find the init of B if it is not
then it will go for a ok that's the
point you'd remember but what if you
also want to call the idiot of is it
possible I mean if I get opposite of B
can I call the in it of both the classes
I mean a and B see by default it will
call only be right what if I were to
call a as well and that's well we have a
very special keyword order method you
can say and that is super so with the
help of super so you can say super and
it's a method basically so you will say
super dot the moment you say super you
can access all the features of the
parent class so you get the super dot in
it so what we are doing is we are trying
to call a init method of Class A so the
moment you say super you are
representing this super class ok but in
this case it is a so we are trying to
call the init method of class a that's
important and now let's run this code
and you can see we got both the output
we
in a and and we got in B and so point to
remember is when you create object of B
it will call the unit of B first and
from the unit of B you are trying to
call the unit of a so it will jump up it
will execute the unit of a first which
will print in a edit and it will come
back to print in B in it okay that's why
we got the output so this makes sense so
we can work with constructor in
inheritance right now let me add a twist
here so what I will do is I will remove
this super from here let's clean it and
I will not even say that this is B so
Class A and Class B two different things
okay they are not linked with each other
but then we have Class C as we have done
earlier will say Class C and Class C
will inherit both the classes a and B
okay and now if I say def and in it you
know right what we are trying to do here
first of all will print in C in it okay
and then I will create object of C so
there is no object of a and B are trying
to object of C now what you think will
it call the edit of C or a or b of
course right when you say the object is
of C it will call the init of C only
let's run this code and that's why we
got the output as in C in it so this
works perfectly but what if you want to
call the init method of a superclass but
there's a twist here now C has two super
classes right one is a and B the moment
you say super dot in it now what do you
think which I need it will cause well if
call it in it of A or B that's a
question let you want this and let's see
what happens I want me to send this on
this quad you can see it says in a in it
that means we are unfair here we are
biased towards a we are not taking be
here right this is completely wrong well
but the thing is we have a concept of em
odd Oh which is method resolution order
so what happens is whenever you have
this multiple inheritance it will always
starts from left to right which means so
first so the moment you say in it it
will try to find the in it of itself so
since we have in it here it will execute
the it of C then the moment you say
super dot and it now we have two classes
like a
be and on the left side we have a and
all that aside we have B so it will
prefer left one first so it goes from
left to right and then something have
remember it's always from left to right
okay and that's why we got in a in it
the same thing can be done for methods
example let's say we have two methods
which are same so in this case you can
see we have feature one in a and feature
one in B both are the same methods with
the same name of course and in this I
will print some different message here I
will print feature one a and here in
face I will say feature one B that means
the feature one is in a and feature one
is B and now if I try to call so I will
say a one dot feature one will it call
from a or b that's a question and we
know the answer right it will always
call from a because it goes from left to
right and you can see the output so
there is something have to remember
which is a concept of method resolution
order so basically in this video we have
talked about three things the first one
is how constructor behaves in in itens
the second one is how to use that super
method and the third one is mro which is
method resolution order now in fact with
the help of super method it can be
called function let's see that so let's
say from we have a function here which
is deaf and I will say feature feed
that's it
and in this I am trying to call the
method of super class right so we can
say super dot and we can use that thing
okay so you can say super dot feature
too and let's say please working
let me call feature it should call the
feature two so yes it works
so you can also use super method to call
other methods as well not just in it so
to represent this super class we use
super method okay so that's secure to
remember so that's it from this video I
hope you are enjoying this series let me
know in the comment section and do
subscribe for for the videos
浏览更多相关视频
SUPER() in Python explained! 🔴
Inheritance in Python | Python Tutorials for Beginners #lec89
Java Inheritance | Java Inheritance Program Example | Java Inheritance Tutorial | Simplilearn
Python OOP Tutorial 1: Classes and Instances
DAY 07 | PHP AND MYSQL | VI SEM | B.CA | CLASS AND OBJECTS IN PHP | L1
Class Inheritance | Godot GDScript Tutorial | Ep 17
5.0 / 5 (0 votes)