Python for Beginners: The Absolute Beginners Guide to Python Programming, Data Science and Predictive Model. A Practical Introduction to Object Oriented Programming Language. (Essentials Cookbook) by Django Smith

Python for Beginners: The Absolute Beginners Guide to Python Programming, Data Science and Predictive Model. A Practical Introduction to Object Oriented Programming Language. (Essentials Cookbook) by Django Smith

Author:Django Smith [Smith, Django]
Language: eng
Format: azw3, epub
Published: 2019-06-12T16:00:00+00:00


Chapter 7: Creating Inheritances in the Python Language

Working with some inheritances in your code can make a big difference in how things line up and work. In fact, these inheritances are going to be a good way to enhance any of the codes that you would like to work within Python. They are going to come into the code and save you a lot of time, while still ensuring that your code looks clean and nice along the way. Any programmer is able to do this by reusing a part of their previous code, without having to go through and rewrite the same code over and over again.

Basically, when you are ready to work with an inheritance in Python, it is time to take the original code, which is going to be known as the parent code here, and then change up parts of it before you reuse it in the derived or the child class. You are able to make any of the changes that you need to the child class to get it to work the way that you would like. Even as someone who is just starting with the Python process, you will be able to use the inheritances to help you rewrite different parts of your code over and over again.

During one of these inheritances, you are going to take the parent code, which is that original code, and copy it over into a new part of the program. This is then going to be the child code. With this child code, you can mess around with it and make it stronger or make other changes as you would wish. In some cases, you will want to copy it down as it is, and other times you may want to change up something inside of it to make the code work the way that you would like it to work.

To make a bit more sense out of some of the inheritances that you can do, and even out of how you can work with these inheritances, let’s take a moment here to look at the code below to see exactly how these will work:

#Example of inheritance

#base class

class Student(object):

​ def__init__(self, name, rollno):

​ self.name = name

​ self.rollno = rollno

#Graduate class inherits or derived from Student class

class GraduateStudent(Student):

​ def__init__(self, name, rollno, graduate):

​ Student__init__(self, name, rollno)

​ self.graduate = graduate

def DisplayGraduateStudent(self):

​ print”Student Name:”, self.name)

​ print(“Student Rollno:”, self.rollno)

​ print(“Study Group:”, self.graduate)

#Post Graduate class inherits from Student class

class PostGraduate(Student):

​ def__init__(self, name, rollno, postgrad):

​ Student__init__(self, name, rollno)

​ self.postgrad = postgrad

​ def DisplayPostGraduateStudent(self):

​ print(“Student Name:”, self.name)

​ print(“Student Rollno:”, self.rollno)

​ print(“Study Group:”, self.postgrad)

#instantiate from Graduate and PostGraduate classes

​ objGradStudent = GraduateStudent(“Mainu”, 1, “MS-Mathematics”)

​ objPostGradStudent = PostGraduate(“Shainu”, 2, “MS-CS”)

​ objPostGradStudent.DisplayPostGraduateStudent()

When you type this into your interpreter, you are going to get the results:

(‘Student Name:’, ‘Mainu’)

(‘Student Rollno:’, 1 )

(‘Student Group:’, ‘MSC-Mathematics’)

(‘Student Name:’, ‘Shainu’)

(‘Student Rollno:’, 2)



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.