First, several concepts
class: represents a collection of objects.
objects: objects include attributes and methods. An attribute is simply a variable that is part of an object, and a method is a function stored inside the object. All objects belong to a class, which is called an instance of a class.
method: A function that is bound to an object attribute is called a method.
Encapsulation: a principle that points to the specific implementation details of hidden objects in other parts of the program.
Inheritance: A class can make a subclass of one or more classes. Subclasses inherit all methods of the superclass,
Subclass: When an object belongs to a class that is a subset of the class to which another object belongs, the former is called the subclass of the latter, which is the superclass of the former.
Second, class
2.1 Creating a Class
Before you create a class, you'll start by saying two concepts.
New class: Creating a modern class creation must have a parent class that, if not specified, inherits object by default.
Legacy class: A class that has no parent object.
There is no legacy class after Python3.0, the previous version, when creating the class, you need to indicate that you inherit the new class, and add __metaclass__ = type where the module or script begins.
class ClassName: ' class Help Information ' # class Document String # class Body
1. You can create a class by using the class statement.
2. Similar to defining functions, you can add help information about the class under the class name and view it through classname.__doc__.
2.2 Self Description
There is only one special difference between a method of a class and a normal function--they must have an extra first parameter name, according to the Convention its name is self.
class Test: def fun1 (self): xxxx def fun2 (self, par1, par2): xxxx
Self stands for the object itself, and of course you can name it, and other names can. With self, the other member methods can access the object itself to manipulate its effects.
When calling a method, you do not need to display the parameter that provides self.
2.3 Privatization
To make a method or feature private (inaccessible from outside), simply precede its name with a double underline.
class Test: def __fun1 (self): xxxx def fun2 (self): self . __fun1 ()
As shown in the example above, __FUN1 is inaccessible from outside, but can be used inside the class. But this is not really inaccessible, because the name with the double underscore is in the form of _test_fun1. So it can be accessed through the TEST._TEST_FUN1 () Form.
Previously underlined names are not imported by an import statement with an asterisk (from module import *).
2.4 Specifying a superclass
class Test (object): xxxx
You can specify a superclass by writing other class names in parentheses after the class statement. Inheriting the superclass will inherit the corresponding method, of course, subclasses can also be overridden, in subclasses, and the superclass of the same name method will override the superclass method.
| Issubclass (subclass name, parent class name) |
You can check whether a class is a subclass of another. Of course, you can also view the base class of a class through the property __bases__ |
| Isinstance (object name, class name) |
You can check whether an object is an instance of a class You can see which class the object belongs to by using the property __class__ |
Subclasses can also inherit multiple superclass, which is called multiple inheritance.
class Test (OBJECT1,OBJECT2): xxxx
It is important to note that when inheriting multiple superclass containing the same name method, the method in the first inherited class overrides the inherited method of the Le species.
?
Python Learning-Classes and objects