#56 Python Tutorial for Beginners | Constructor in Inheritance

Telusko
28 Aug 201808:28

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

00:00

πŸ‘¨β€πŸ’» 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.

05:00

πŸš€ 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

Inheritance in object-oriented programming (OOP) allows one class (subclass) to acquire the properties and behaviors (methods) of another class (superclass). In the video, the concept of inheritance is central to understanding how Class B inherits features from Class A, and later, how multiple inheritance allows Class C to inherit features from both A and B. This enables code reuse and hierarchical relationships between classes.

πŸ’‘Superclass

A superclass is the class whose properties and methods are inherited by another class. In the video, Class A serves as a superclass from which Class B inherits properties and methods. The term illustrates the hierarchical relationship between classes, where a subclass extends the capabilities of a superclass.

πŸ’‘Subclass

A subclass is a class that inherits properties and behaviors from a superclass. In the script, Class B is a subclass of Class A, meaning it has access to the features of A. This concept is key in demonstrating how inheritance works in OOP, as a subclass can extend or override functionalities of the superclass.

πŸ’‘Constructor

A constructor is a special method in a class, typically called `__init__` in Python, that initializes the object of that class when it is created. The video discusses how constructors behave in inheritance, specifically how creating an object of a subclass (Class B) can also invoke the constructor of its superclass (Class A), unless the subclass has its own constructor defined.

πŸ’‘Method Resolution Order (MRO)

Method Resolution Order (MRO) is the sequence in which Python looks for a method or attribute in a hierarchy of classes, especially in cases of multiple inheritance. In the video, MRO is used to explain how, in multiple inheritance, Python checks from left to right (Class A first, then Class B) when trying to resolve a method like `__init__`. This order ensures a predictable lookup path for methods.

πŸ’‘Multiple Inheritance

Multiple inheritance is a feature in OOP where a class can inherit properties and methods from more than one superclass. In the video, Class C is shown inheriting from both Class A and Class B, demonstrating how features from both can be used in Class C, though MRO determines the order in which methods from the superclasses are called.

πŸ’‘Super

The `super` keyword in Python is used to refer to the superclass and allows calling its methods or accessing its attributes. In the video, `super` is used to call the constructor (`__init__`) of the superclass within the subclass, ensuring that the superclass's initialization occurs even when the subclass has its own constructor.

πŸ’‘Feature

A feature in this context refers to a method or attribute that a class possesses. The video repeatedly mentions that Class B inherits 'features' from Class A, meaning that it can access all methods and attributes defined in Class A. Features are central to inheritance, as they define what behaviors or properties can be reused.

πŸ’‘Parent Class

A parent class, also known as a superclass, is the class from which other classes (subclasses) inherit. In the video, Class A is considered the parent class of Class B and C, as both B and C derive functionality from A. This terminology is used to establish relationships between classes in inheritance hierarchies.

πŸ’‘Object

An object is an instance of a class in OOP, created using a class constructor. In the video, the process of creating objects from Class A, Class B, and Class C is discussed. The object `a1` from Class A can access only features of A, while an object of Class B can access features from both B and its parent class, A. Objects are crucial in understanding how class features are used in practice.

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

play00:00

[Music]

play00:02

welcome back aliens my name is Ivan 20

play00:05

and let's continue the series on Python

play00:07

now to this fine we have talked about

play00:09

oops concept right and which we have

play00:11

seen inheritance right now what is

play00:14

inheritance if you already have some

play00:15

classes and if you want to use the

play00:17

existing features you will simply say

play00:20

class B which will take the features

play00:22

from a that so class b inherits a or you

play00:26

can go with a concept of multiple

play00:28

inheritance where one class will extend

play00:31

from two different classes all more than

play00:33

two classes right so in this case we

play00:35

have Class A Class B and then we have

play00:36

plus C which is 20 which has a feature 5

play00:39

but it is also inheriting a and B so we

play00:41

can say C has five features right so

play00:44

that makes sense now what do I do in

play00:45

this video is we'll talk about two

play00:47

topics the first one is the constructor

play00:50

in inheritance how it behaves and we

play00:52

will also talk about method resolution

play00:55

order all you can say mr o so let's

play00:57

start the constructor time bin let's

play00:59

ignore C ok so let's say we don't have

play01:02

this C something note that part and in

play01:05

fact will delete that part from for time

play01:07

bin and this focus only on a and B where

play01:10

the B is inheriting a ok so in total in

play01:13

B we have 4 features right now what

play01:15

happens is if I create an object of a so

play01:17

let's say if I say a1 is equal to

play01:20

constructor of a so this will work right

play01:22

so when you say a bracket that's a

play01:23

constructor it will call a method which

play01:25

is in it now even if you don't define it

play01:27

it is there somewhere right but let me

play01:29

define my own constructor or in it here

play01:31

so I will say def and will define the

play01:33

heat method and this in it meter will

play01:35

print one stuff so let me say print in a

play01:38

in it nothing much we are not doing

play01:40

anything extra here we are just printing

play01:41

in a in it that's it now what do you

play01:44

think what it will print of course we

play01:45

are creating object of a right so it

play01:47

will only execute a C with the object of

play01:49

a you cannot access features of B ok so

play01:51

even if I try example if I say a1 dot

play01:54

you can only access feature 1 and

play01:56

feature 2 but yes if you have an object

play01:58

of B you can access all the features

play02:00

right or features from a and B because b

play02:03

is a subclass and a is superclass and as

play02:06

I mentioned subclass can access all the

play02:08

features from superclass but that's not

play02:10

wise versa right a superclass cannot

play02:12

access

play02:12

the visas of subclass not even one which

play02:14

are subclass okay this is fun

play02:15

now what if I create object of B so I'm

play02:18

not going to object of a here putting on

play02:20

stuff P doesn't matter what is a 1 or B

play02:22

1 important is if I create an object B

play02:24

will call the constructor that's the

play02:26

question because we are not getting the

play02:27

object of a we are creating almost of B

play02:29

will it Paul the constructor of a that's

play02:32

the question let me run this code and it

play02:34

works ok so even if you have the object

play02:36

of B it will still call the constructor

play02:38

of a right that's how it works but what

play02:41

if you have your own constructor what if

play02:44

you have your own init method example in

play02:46

be seen B we don't have that in it right

play02:48

and that's what it's going up so let me

play02:49

repeat since we don't have in it inside

play02:52

B that's why it is going to a but what

play02:55

if you already have in it with you so I

play02:58

will simply copy paste here in B so both

play03:00

have in it but then in this I'll print

play03:02

in B in it right so in a via printing in

play03:05

a in it and B we are printing in be in

play03:07

it so this makes sense now wafer than

play03:10

this code what it will print it will it

play03:11

print in a in it or in B in it

play03:14

of course right if you are cutting of it

play03:16

of B it should only call the init of P

play03:18

right and let's see if that works and

play03:20

the test on this code and if we alright

play03:22

so when you run the code you can see we

play03:24

only got in be in it so that means if

play03:27

you create an object of B first it will

play03:30

try to find the init of B if it is not

play03:33

then it will go for a ok that's the

play03:35

point you'd remember but what if you

play03:37

also want to call the idiot of is it

play03:40

possible I mean if I get opposite of B

play03:42

can I call the in it of both the classes

play03:45

I mean a and B see by default it will

play03:48

call only be right what if I were to

play03:50

call a as well and that's well we have a

play03:53

very special keyword order method you

play03:54

can say and that is super so with the

play03:57

help of super so you can say super and

play03:59

it's a method basically so you will say

play04:02

super dot the moment you say super you

play04:04

can access all the features of the

play04:07

parent class so you get the super dot in

play04:08

it so what we are doing is we are trying

play04:10

to call a init method of Class A so the

play04:13

moment you say super you are

play04:14

representing this super class ok but in

play04:17

this case it is a so we are trying to

play04:19

call the init method of class a that's

play04:22

important and now let's run this code

play04:24

and you can see we got both the output

play04:25

we

play04:26

in a and and we got in B and so point to

play04:30

remember is when you create object of B

play04:31

it will call the unit of B first and

play04:34

from the unit of B you are trying to

play04:36

call the unit of a so it will jump up it

play04:39

will execute the unit of a first which

play04:40

will print in a edit and it will come

play04:43

back to print in B in it okay that's why

play04:45

we got the output so this makes sense so

play04:48

we can work with constructor in

play04:49

inheritance right now let me add a twist

play04:52

here so what I will do is I will remove

play04:54

this super from here let's clean it and

play04:56

I will not even say that this is B so

play05:00

Class A and Class B two different things

play05:02

okay they are not linked with each other

play05:03

but then we have Class C as we have done

play05:05

earlier will say Class C and Class C

play05:08

will inherit both the classes a and B

play05:11

okay and now if I say def and in it you

play05:16

know right what we are trying to do here

play05:17

first of all will print in C in it okay

play05:21

and then I will create object of C so

play05:23

there is no object of a and B are trying

play05:25

to object of C now what you think will

play05:28

it call the edit of C or a or b of

play05:31

course right when you say the object is

play05:33

of C it will call the init of C only

play05:35

let's run this code and that's why we

play05:38

got the output as in C in it so this

play05:40

works perfectly but what if you want to

play05:43

call the init method of a superclass but

play05:46

there's a twist here now C has two super

play05:49

classes right one is a and B the moment

play05:52

you say super dot in it now what do you

play05:55

think which I need it will cause well if

play05:57

call it in it of A or B that's a

play05:59

question let you want this and let's see

play06:01

what happens I want me to send this on

play06:02

this quad you can see it says in a in it

play06:04

that means we are unfair here we are

play06:06

biased towards a we are not taking be

play06:08

here right this is completely wrong well

play06:12

but the thing is we have a concept of em

play06:13

odd Oh which is method resolution order

play06:17

so what happens is whenever you have

play06:19

this multiple inheritance it will always

play06:21

starts from left to right which means so

play06:24

first so the moment you say in it it

play06:26

will try to find the in it of itself so

play06:29

since we have in it here it will execute

play06:31

the it of C then the moment you say

play06:34

super dot and it now we have two classes

play06:36

like a

play06:36

be and on the left side we have a and

play06:39

all that aside we have B so it will

play06:41

prefer left one first so it goes from

play06:43

left to right and then something have

play06:45

remember it's always from left to right

play06:49

okay and that's why we got in a in it

play06:51

the same thing can be done for methods

play06:53

example let's say we have two methods

play06:56

which are same so in this case you can

play06:57

see we have feature one in a and feature

play06:59

one in B both are the same methods with

play07:02

the same name of course and in this I

play07:04

will print some different message here I

play07:06

will print feature one a and here in

play07:09

face I will say feature one B that means

play07:11

the feature one is in a and feature one

play07:13

is B and now if I try to call so I will

play07:16

say a one dot feature one will it call

play07:19

from a or b that's a question and we

play07:22

know the answer right it will always

play07:23

call from a because it goes from left to

play07:26

right and you can see the output so

play07:28

there is something have to remember

play07:29

which is a concept of method resolution

play07:31

order so basically in this video we have

play07:33

talked about three things the first one

play07:35

is how constructor behaves in in itens

play07:38

the second one is how to use that super

play07:40

method and the third one is mro which is

play07:43

method resolution order now in fact with

play07:46

the help of super method it can be

play07:48

called function let's see that so let's

play07:50

say from we have a function here which

play07:52

is deaf and I will say feature feed

play07:55

that's it

play07:55

and in this I am trying to call the

play07:57

method of super class right so we can

play08:00

say super dot and we can use that thing

play08:03

okay so you can say super dot feature

play08:05

too and let's say please working

play08:06

let me call feature it should call the

play08:08

feature two so yes it works

play08:10

so you can also use super method to call

play08:12

other methods as well not just in it so

play08:14

to represent this super class we use

play08:16

super method okay so that's secure to

play08:19

remember so that's it from this video I

play08:20

hope you are enjoying this series let me

play08:23

know in the comment section and do

play08:24

subscribe for for the videos

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Python TutorialInheritanceConstructorsMethod Resolution OrderSuper ClassObject-Oriented ProgrammingOOP ConceptsProgramming BasicsTech EducationCode Examples