Example analysis of Python class inheritance usage _python

Source: Internet
Author: User

This example describes the Python class inheritance usage. Share to everyone for your reference. The specific method is as follows:

#!/usr/bin/python # Filename:inherit.py Class Schoolmember: ' represents any school member. ' def __init__ (self, Name, age): Self.name = name Self.age = Age print ' (initialized Schoolmember:%s) '% Self.nam
    E def tell (self): "' Tell me details. '"
  print ' Name: '%s ' Age: '%s '% (Self.name, self.age), Class Teacher (Schoolmember): ' Represents a Teacher. ' def __init__ (self, name, age, salary): schoolmember.__init__ (self, name, age) Self.salary = salary print ' (init Ialized Teacher:%s) '% Self.name def tell (self): Schoolmember.tell (self) print ' Salary: '%d '% self.salary clas
  S Student (Schoolmember): ' Represents a Student. ' def __init__ (self, name, age, marks): schoolmember.__init__ (self, name, age) Self.marks = Marks print ' (Initial ized Student:%s) '% Self.name def tell (self): Schoolmember.tell (self) print ' Marks: '%d '% self.marks t = Teach ER (' Mrs shrividya ', 40,30000) s = Student (' Swaroop ', 22,75) members = [T, s] for \ Members:member.tell () # Works for both Teachers and Students

 

The results of running the output are as follows:

(initialized Schoolmember:mrs. Shrividya)
(initialized Teacher:mrs. Shrividya)
(initialized Schoolmember:swaroop)
(initialized Student:swaroop)
Name: "Mrs. Shrividya" Age: "Salary": "30000"
Name: "Swaroop" Age: "Marks": "75"

How it works

To use inheritance, we follow the name of the base class as a tuple after the class name when the class is defined. We then note that the __init__ method of the base class is specifically invoked with the self variable, so that we can initialize the base class part of the object. This is very important--python will not automatically invoke the basic class constructor, you have to specifically call it yourself.

We also observed that we prefix the method call with the class name, and then pass the self variable and other arguments to it.

Note that when we use the Tell method of the Schoolmember class, we take teacher and student instances only as instances of Schoolmember.

In addition, in this example, we call the subtype's Tell method, not the Schoolmember class's Tell method. As you can see, Python is always looking for the corresponding type first, in this case. If it cannot find the corresponding method in the derived class, it begins to look in the base class one by one. The base class is specified in the tuple when the class is defined.

Annotation of a term- if more than one class is listed in an inherited tuple, it is called multiple inheritance .

I hope this article will help you with your Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.