class The basic knowledge point record of a class
1. Old class and new type
-python's new class was introduced in 2.2, and the previous classes were called Old classes or classic classes. Before 2.2, classes and types were two different concepts, introducing new classes to unify the two (I didn't see what it meant.
thinking). The base-class search mechanism for new classes is breadth-first traversal, while the base-class search mechanism for legacy classes is depth-first traversal. By default, user-defined classes are legacy classes, and if you need to define a new class, there are two ways:
#在定义新式类前, put the following sentence at the front of the module __metaclass__ = Type #直接或间接的继承object类 class NewObject (object): Def __init__ (self): pass 2.self-In The default first argument in a class-defined function is self, the class itself, where the function automatically passes in the class as a parameter each time the function is invoked. In addition, if you need to invoke a property in a class, you also need to invoke it by self, the parameter self. The-self parameter is the difference between the method and the function. Method binds the first parameter to the instance to which it belongs, so this parameter can be passed in without the caller's display. 3. Private-python do not support private mode. However, there are a few tricks that you can use to avoid external direct access to these properties or methods. -To make a method or feature private (inaccessible from the outside), precede its name with a double underline:
Class Test:def __method1 (self,x): print x execution
>>> test = Test () >>> test.__method1 (' a ') traceback (most recent call last): File "<pyshell#22>" , line 1, in <module> test.__method1 (' a ') Attributeerror:test instance has no attribute ' __method1 '-principle, class of internal definition, all to The names that begin with a double underline are "translated" into the form of a single underline and a class name.
>>> class Test:def __method1 (self): print ' a ' >>> test = Test () >>> test <__main__. Test instance at 0x0000000002ad7e08> >>> test._test__method1 () a >>> test.__method1 () traceback (Mo St recent Call last): File "<pyshell#32>", line 1, in <module> test.__method1 () Attributeerror:test instance has no attribute ' __method1 '-in addition, you can use a single underline if you do not need such a method but want other objects to not access the internal data. The underlined name will not be imported by an Imports statement with an asterisk
From module Import * 4. Superclass-when defining a class, the class name is followed by parentheses, the superclass class name to inherit in parentheses, and the class name separated by commas. -Note multiple inheritance, if a method inherits from multiple superclass, you need to pay attention to the order of these superclass, the methods in the inherited class will override the methods in the inherited class. This looks like a weird one.
>>> class Classa:def Talk (self): print ' This is A talking ' >>> class Classb:def Talk (self): print ' t He is B talking ' >>> class CLASSC (CLASSA,CLASSB): def ctalk (self): print ' C is talking ' >>> C = Cla SsC () >>> C.talk () is A talking 5. Interface-python There is no need to display the definition interface ... Unlike Java, it is not necessary to show what the specified object must contain to be used as a parameter to a method ... You can assume that the interface is already implemented when you use the object, and if not, the program will fail ...-there are several ways to check whether the method exists, and whether it can be invoked. -hasattr (object name, method name) method that determines whether a method exists within the object
>>> class Classa:def func1 (self): print ' This is func1 ' >>> cla_a = ClassA () >>> Hasat TR (cla_a, ' func1 ')