Python class definition and class inheritance

Source: Internet
Author: User

Python class definition and class inheritance

I. Class Definition:

Class <class name >:< Statement>

After the class is instantiated, you can use its attributes. In fact, after creating a class, you can access its attributes through the class name.
If you directly use the class name to modify its attributes, it will directly affect the instantiated object.
 
Private attributes of the class:
_ Private_attrs starts with two underscores and declares that this attribute is private and cannot be used outside the class or accessed directly.
Self. _ private_attrs used in internal methods of the class
 
Class Method
Inside the class, you can use the def keyword to define a method for the class. Unlike the general function definition, the class method must contain the self parameter and be the first parameter.
 

Private Class Method
_ Private_method starts with two underscores and declares this method as a private method. It cannot be called outside the class.
Call slef. _ private_methods inside the class
Class proprietary method:
_ Init _ constructor called when an object is generated
_ Del _ destructor used to release objects
_ Repr _ print, convert
_ Setitem _ assign values based on the index
_ Getitem _ obtain the value based on the index
_ Len _ Get the length
_ Cmp _ comparison Calculation
_ Call _ function call
 
_ Add _ addition operation
_ Sub _ Subtraction
_ Mul _ Multiplication operation
_ Div _ division operation
_ Mod _ remainder operation
_ Pow _ caller
Example:

# Class definition class people: # define basic attribute name = ''age = 0 # define private attributes, private attributes cannot be directly accessed outside the class _ weight = 0 # define the constructor def _ init _ (self, n, a, w): self. name = n self. age = a self. _ weight = w def speak (self): print ("% s is speaking: I am % d years old" % (self. name, self. age) p = people ('Tom ', 10, 30) p. speak ()

Ii. Inheritance class definition:

1. Single inheritance

Class <class Name> (parent class name) <Statement> eg. class childbook (book) age = 10

2. Multi-inheritance of Classes

Class name (parent class 1, parent class 2,..., parent class n) <Statement 1>


Note the sequence of the parent class in parentheses. If the parent class has the same method name but is not specified in the subclass, search for python from left to right, that is, if the method is not found in the subclass, you can check from left to right whether the parent class contains the method.

# Another class, prepare class speaker () for multi-inheritance: topic = ''name = ''def _ init _ (self, n, t): self. name = n self. topic = t def speak (self): print ("I am % s, I am a speaker! My topic is % s "% (self. name, self. topic) # multi-inheritance class sample (speaker, student): a = ''def _ init _ (self, n, a, w, g, t): student. _ init _ (self, n, a, w, g) speaker. _ init _ (self, n, t) test = sample ("Tim", 25, 80, 4, "Python") test. speak () # The method name is the same. By default, the method of the parent class is called in brackets.

Override of class methods -- The subclass overwrites the parent class method.
Def method name is consistent with parent class
If you want to use the parent class method parent class name in the method. Method Name
 
If the class is put in the module
 
Use Time

Import Al = A. Class ()

Execution results of the above three procedures:


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.