Inheritence of classes in Python

Python Programming More of Python
13 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.22
List Price:  €93.18
You save:  €27.95
£56.04
List Price:  £80.07
You save:  £24.02
CA$96.09
List Price:  CA$137.28
You save:  CA$41.18
A$106.44
List Price:  A$152.07
You save:  A$45.62
S$94.93
List Price:  S$135.63
You save:  S$40.69
HK$547
List Price:  HK$781.46
You save:  HK$234.46
CHF 63.65
List Price:  CHF 90.94
You save:  CHF 27.28
NOK kr765.05
List Price:  NOK kr1,092.98
You save:  NOK kr327.92
DKK kr486.48
List Price:  DKK kr695
You save:  DKK kr208.52
NZ$116.68
List Price:  NZ$166.69
You save:  NZ$50.01
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,685.71
List Price:  ৳10,980.05
You save:  ৳3,294.34
₹5,844.71
List Price:  ₹8,349.95
You save:  ₹2,505.23
RM331.80
List Price:  RM474.03
You save:  RM142.22
₦98,423.43
List Price:  ₦140,610.93
You save:  ₦42,187.50
₨19,474.95
List Price:  ₨27,822.55
You save:  ₨8,347.60
฿2,584.73
List Price:  ฿3,692.63
You save:  ฿1,107.90
₺2,255.06
List Price:  ₺3,221.65
You save:  ₺966.59
B$356.31
List Price:  B$509.04
You save:  B$152.73
R1,298.99
List Price:  R1,855.78
You save:  R556.79
Лв127.57
List Price:  Лв182.26
You save:  Лв54.68
₩95,950.21
List Price:  ₩137,077.60
You save:  ₩41,127.39
₪261.40
List Price:  ₪373.45
You save:  ₪112.04
₱4,015.50
List Price:  ₱5,736.67
You save:  ₱1,721.17
¥10,912.42
List Price:  ¥15,589.84
You save:  ¥4,677.42
MX$1,187.01
List Price:  MX$1,695.81
You save:  MX$508.79
QR254.79
List Price:  QR364.01
You save:  QR109.21
P956.45
List Price:  P1,366.42
You save:  P409.96
KSh9,168.69
List Price:  KSh13,098.69
You save:  KSh3,930
E£3,313.80
List Price:  E£4,734.21
You save:  E£1,420.40
ብር4,020.93
List Price:  ብር5,744.43
You save:  ብር1,723.50
Kz58,536.36
List Price:  Kz83,626.96
You save:  Kz25,090.59
CLP$65,519.73
List Price:  CLP$93,603.63
You save:  CLP$28,083.90
CN¥505.79
List Price:  CN¥722.59
You save:  CN¥216.80
RD$4,064.05
List Price:  RD$5,806.04
You save:  RD$1,741.98
DA9,434.16
List Price:  DA13,477.95
You save:  DA4,043.79
FJ$159.43
List Price:  FJ$227.77
You save:  FJ$68.34
Q544.12
List Price:  Q777.35
You save:  Q233.22
GY$14,659.33
List Price:  GY$20,942.80
You save:  GY$6,283.47
ISK kr9,804.19
List Price:  ISK kr14,006.59
You save:  ISK kr4,202.40
DH702
List Price:  DH1,002.91
You save:  DH300.90
L1,236.68
List Price:  L1,766.77
You save:  L530.08
ден4,019.08
List Price:  ден5,741.79
You save:  ден1,722.70
MOP$563.89
List Price:  MOP$805.60
You save:  MOP$241.70
N$1,302.92
List Price:  N$1,861.40
You save:  N$558.47
C$2,577.98
List Price:  C$3,682.99
You save:  C$1,105
रु9,357.62
List Price:  रु13,368.60
You save:  रु4,010.98
S/260.83
List Price:  S/372.63
You save:  S/111.80
K271.34
List Price:  K387.65
You save:  K116.30
SAR262.49
List Price:  SAR375
You save:  SAR112.51
ZK1,913.50
List Price:  ZK2,733.69
You save:  ZK820.18
L324.50
List Price:  L463.60
You save:  L139.09
Kč1,630.34
List Price:  Kč2,329.15
You save:  Kč698.81
Ft25,331.89
List Price:  Ft36,189.97
You save:  Ft10,858.07
SEK kr764.93
List Price:  SEK kr1,092.80
You save:  SEK kr327.87
ARS$61,714.55
List Price:  ARS$88,167.42
You save:  ARS$26,452.87
Bs483.86
List Price:  Bs691.26
You save:  Bs207.40
COP$272,553.83
List Price:  COP$389,379.31
You save:  COP$116,825.47
₡35,845.57
List Price:  ₡51,210.15
You save:  ₡15,364.58
L1,730.57
List Price:  L2,472.36
You save:  L741.78
₲523,213.21
List Price:  ₲747,479.48
You save:  ₲224,266.27
$U2,704.33
List Price:  $U3,863.50
You save:  $U1,159.16
zł280.23
List Price:  zł400.34
You save:  zł120.11
Already have an account? Log In

Transcript

In this lesson, we'll take a look at how in the narratives within classes works. So, basically the main advantage of inheritance is that you can add a new method to a class without having to change the original definition of it. So, let me give you an example. Let us suppose that we define a class and call it dogs. So the common attribute of dogs could be eating, running, sleeping, and so on. So first dogs are animal so you first should find basic characteristics common for all animals, such as eating and sleeping.

In this way, you don't have to save the attributes for each and every kind of animal. So we say we can see that animals are the parent class of dogs and dogs are the same. class or we can call it as child class of animals. So, this is the concept we defined as inheritance, it is very important to understand it as it is very powerful concept of object oriented programming in Python, with the concept of inheritance, some kind of programs can be written much easier and faster, such as creating characteristics of gains, as we saw in our previous lesson, or all those cases where we have to create many, many similar objects. So now let's go and do a few practical examples. So let me first define a class.

So here I will write class and then here I write parent. I'll give a call now define a number here. So I'll define number is equal to 10. This is the value that I'm Creating that is a variable am creating an assigning a value 10 to that particular variable. Now, as we have already know about class, so, we know after assigning a variable, we have to use def function that is you have to define what init function that is initialization function with double underscore now write self. Now, here you need to print this that is this is parent class.

So, for every time we call this class we must treat that this is parent class. At this point, we define the common function within the parentless writing so, data And then here in the next thing what I have to do is I will write def, and then I'll give a name called first function. Now, here I will give the self parameter again colon and again I have to give the printed structure. So, here left and right, this is the first function of gradient class. So, here this will print, this is the first function of parent class. So, this is just an example, too late you properly understand the mechanism of this operation.

Then after we define function where we can put in the attribute in this way. So now here I will define a function where I can put that attribute. So for that I'm right define then set a term then self, comma sutra, give a semicolon. And here what you have to write. So in the previous lesson, we have assigned parameter attributes to a name that and this time we'll call it parent attribute. So here what I am right is I will write parent dot parent attribute which is equal to better Now, after this, I will again define attribute, which will help me to see the particular value of that attribute that is, here I'll give an input.

And what input I have given will be displayed in the next step. So for that here I'll define get attribute. So here right? Get a pair. Then within bracket write self, and giver column. Then write, print, and write within quotes.

Parent. attribute. Eva Coleman here And right parent dot parent attribute. Okay, now, so having done this, now let's define the child class let's write here and define in the child class. So I'll write class then here I'll write child and now as this is the child class of the parent class, so here within this first bracket, I write parent. So it will define that this child class is the sub class of this parent.

So, this child is the subclass of this class parent. So I've written parent here. So again, give cola Next, right here I'll define again initialize any by init function to initialize the self, then whenever I'll call this, it should print let's say, this is the giant class. Now, yet I again define a function so I'll write def. Second form. We didn't go racket right self, even a colon, come to the next slide and write print.

This is second function of child class. Okay, so here, I'll run this program now. So it's running, so it ran correctly without any error. So my two classes are now created one is parent and the subclass that is created is child. Now here I create few variable. So let's create c one equal to giant.

So here, I just run this giant, the first bracket, and I run this What should we print, it should print that this is the child class. Let's see whether it prints or not. So as it prints, this is the child class. So basically, what I want to show you is creating this variable is that when we assign the child class to it, it can use both the parent class and the child class. So to prove it, I'll do here, C underscore one dot. First.

Fun typing it. First fun. Now you're getting the next length right see underscore one dot second. Fun. Okay, now, again come in the next line. Let's see underscore one dot here I will use set actor.

Now, in the same data I need to give a value because the data will always asks for a value. So here I'll write two 50 Okay, now, here I'll write C underscore one dot get again a pair of first bracket. So, here I have provided a value because here it needs a value now run this. So what it printed is this is the first function of parent class. Now, can you tell me why this error is showing? See it is indicating to the second line that is see one second function, why it is showing me this error damages Tell me because here, what I've done is here, I'm initializes second function is is in small letter here I've written is in a capital letter.

So that's why it's showing me the error. So just change it and run it again. See, now it printed everything. So this is the first function of the In class C, I've written c one c one is assigned to the child class. Now, I have written first function first function is from the parent class, so, it can access parent class also. Now, one second function it is from the child class and it is taking the value or taking the attribute to 50 and here in the gate at what it is printing parent attribute 250.

So, in this way this child note or you can see this child class can use both the parent class and design class. Now, if you want to change the value of this first function and want to incorporate something in the child class, so, for that what you can do is you can just define first function here. So, first function self you have a colon here, come to the next line and then Print changed, okay. Now whenever first function will be called, it will print changed. So just run this program and see if there is any change or not before running this program you need to run this program because here I have done a change. So, run this program first it's run now come to this section and run this program see what it print now it has printed changed now the first function that is from the parent class, it hasn't been don't print it, this is first function for parent class, it printed what it printed from the child class it printed changed from that child class.

So, here we have changed the class that is from the parent class we have changed to the child class and now it is running within this child class. So this is the end of this lesson. So we have seen the working principle of inheritance among the classes So defining that the class is the class of an honor, it is enough to write the name of the parent class between brackets and automatically the child class will take all its attributes. And lastly, we have seen that if you want to add a function of the parent class, we can just rewrite it with the same name in the child class, and the value will be changed. So keep practicing. See you the next video.

Thank you.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.