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
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.epub
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.
Ada | Ajax |
Assembly Language Programming | Borland Delphi |
C & C++ | C# |
CSS | Compiler Design |
Compilers | DHTML |
Debugging | Delphi |
Fortran | Java |
Lisp | Perl |
Prolog | Python |
RPG | Ruby |
Swift | Visual Basic |
XHTML | XML |
XSL |
Hello! Python by Anthony Briggs(9885)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9770)
The Mikado Method by Ola Ellnestam Daniel Brolund(9763)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8270)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7762)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7750)
Grails in Action by Glen Smith Peter Ledbrook(7678)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7534)
Windows APT Warfare by Sheng-Hao Ma(6567)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6393)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6314)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6188)
Kotlin in Action by Dmitry Jemerov(5033)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4304)
Functional Programming in JavaScript by Mantyla Dan(4028)
Solidity Programming Essentials by Ritesh Modi(3878)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3650)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3600)
The Ultimate iOS Interview Playbook by Avi Tsadok(3571)
