Inheritance of Python classes

Source: Internet
Author: User

Some basic concepts of Python classes

Everything in Python, the process of declaring variable functions, is actually a connection to the space allocated to the object in memory. Python, like Java, C + +, has its own properties and methods. A variable that belongs to an object or class is called a domain. There are two types of domains--objects that belong to each instance/class or belong to the class itself, which are called instance variables and class variables, respectively. A variable of a class is shared by all objects (instances) of a class. All objects share a copy of a class variable. The variables of the object are owned by each object/instance of the class. Therefore each object has its own copy of the domain.

There is only one special difference between the methods of classes and normal functions-they must have an extra first parameter name, self, but you do not assign a value to this parameter when calling this method, and Python provides this value. Self is equivalent to this in Java, which means the object itself. If you have a method that does not require parameters, you still have to define a self parameter for this method.

The __init__ method in a class is passed to the __init__ method when it creates a new instance of a class, including the parameters in parentheses followed by the class name. It runs immediately when an object of the class is created. The __init__ method is similar to constructors in C + +, C #, and Java, and can be used to assign values to properties. Like the __init__ method, there is a special method __del__, which is called when the object dies. Object Vanishing Object
is no longer being used, the memory it occupies will be returned to the system for its use. The __del__ method is similar to the concept of destructor.

The following is the inheritance of the Python class

#!/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: 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 PR int ' (Initialized Student:%s) '% Self.name def tell (self): Schoolmember.tell (self) print ' Marks: '%d ' '% SELF.MArks t = Teacher (' Mrs. Shrividya ', +, 30000) s = Student (' Swaroop ',) Print # Prints a blank line members = [T, S]  For member in Members:member.tell () # Works for both Teachers and Students

Python does not automatically invoke the constructor of the base class, you have to call it yourself. The subclass to display the constructor of the calling parent class is the __init__ method. Like Java, Python is also run-time bound , depending on the actual type of object that is called, not the static binding of C + +.

The results of the operation are as follows:




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.