Python class inheritance

Source: Internet
Author: User

Python class inheritance

Some basic concepts of the Python class

In Python, everything is an object. The process of declaring variable functions is actually the connection to the space allocated for objects in the memory. Similar to Java and C ++, Python has its own attributes and methods. A variable of an object or class is called a field. There are two types of domains: The objects belonging to each instance/class or the class itself, which are called instance variables and class variables respectively. Class variables are shared by all objects (instances) of a class. All objects share a copy of a class variable. The object variables are owned by each object/instance of the class. Therefore, each object has its own copy of this domain.

There is only one special difference between class methods and common functions-they must have an additional first parameter name self, but you do not assign a value to this parameter when calling this method, python provides this value. Self is equivalent to this in Java, indicating the object itself. If you have a method that does not require parameters, you must define a self parameter for this method.

When creating a new instance of a class, the _ init _ method in the class includes parameters in parentheses following the class name and passes them to the _ init _ method. It runs immediately when an object of the class is created. The _ init _ method is similar to the constructor in C ++, C #, and Java. It can be used to assign values to attributes. Like the _ init _ method, there is also a special method _ del __, which is called when the object disappears. The object disappears.
It is no longer used, and the memory occupied by it will be returned to the system for its use. The _ del _ method is similar to the destructor method.

The following are python class inheritance

#!/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.name     def tell(self):         '''Tell my 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 '(Initialized Teacher: %s)' % self.name     def tell(self):         SchoolMember.tell(self)         print 'Salary: "%d"' % self.salary class Student(SchoolMember):     '''Represents a student.'''     def __init__(self, name, age, marks):         SchoolMember.__init__(self, name, age)         self.marks = marks         print '(Initialized Student: %s)' % self.name     def tell(self):         SchoolMember.tell(self)         print 'Marks: "%d"' % self.marks t = Teacher('Mrs. Shrividya', 40, 30000) s = Student('Swaroop', 22, 75) print # prints a blank line members = [t, s] for member in members:     member.tell() # works for both Teachers and Students 

Python does not automatically call the constructor of the basic class. You have to call it in person. The constructor of the parent class to be called by the subclass, that is, the _ init _ method. Like Java, Python is also a runtime binding. The method called depends on the actual type of the object, rather than static binding of C ++.

The running result is as follows:




Related Article

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.