The definition format of the class:
Class CLASSNAME (parent Class 1, parent Class 2, parent Class 3):
__a = 0 # #私有属性, two _ starts, only functions in that class can access
B = 0 # #公共属性
def function name (self):
Self.__a = ... #访问私有属性名
Self. B = ... #访问公共有属性名
Ps:
1, when defining the function, must have self this parameter, and must be placed in the first position
2, when defining private functions, function names and private properties, the same as the two _ start
3, the inherited parent class can be multiple, more than one parent class has the same function, if other functions to call this function, then the search from left to right, will only access the first parent class with the function
4. You can override a function in a parent class in a subclass
5, if you want to instantiate or inherit the class of other modules, you must first import the module with import, and then instantiate or inherit
6. The proprietary methods in the class are as follows:
1 |
__init__ |
Constructor that is called when the object is generated. |
2 |
__del__ |
destructor, which is used when releasing objects |
3 |
__setitem__ |
Assigning values by index |
4 |
__repr__ |
Print, convert |
5 |
__getitem__ |
Get values by index |
6 |
__len__ |
Get length |
7 |
__cmp__ |
Comparison operation |
8 |
__add__ |
Add operation |
9 |
__sub__ |
Minus operations |
10 |
__mull__ |
Multiply operation |
11 |
__div__ |
Except operations |
12 |
__mod__ |
To find the remainder operation |
13 |
__pow__ |
exponentiation operation |
14 |
__call__ |
Function call |
python--class