Types and classes:
With the new class, Classa (object) can be of type A, and the new class
Python uses __new__ to construct objects, initialize objects using __init__, which are separated, and can be initialized with the Init method at the same time as new, with arguments, and each function (method) in the class is given the self, represented as a method on the instance. Self represents an instance of this class.
Instance variable: is a variable for each instance, and the variables for each instance are different;
Class variable: is for the class, the variable in the class is the same;
Class A (object): #这个是类变量 author = ' Shizhen ' #初始化方法, what it looks like, is initialized according to this format def __init__ (self,page): #这个是实例变量 SELF.PA = PageA = A (+) B = A ($) print a.pa,b.pa,a.author,b.author# Dynamic language is characterized by the ability to add additional attributes to an object at any time, such as this, the book attribute is added to the A object; a.bo OK = ' OMG ' Print a.book# here the author is not a class variable, but an an object creates an instance variable that accesses the instance variable first; a.author = ' Lixuan ' Print A.author
Class methods and Static methods
Class A (object): Author = ' Shizhen ' #self是实例方法, can be used after instantiation. The first of the functions is self def __init__ (self,page): SELF.PA = page #类方法, plus @, and the first parameter passed in is the CLS, called directly with the class @classmethod def class_method (cls,msg): Return msg #静态方法, add @, directly into the parameters to be passed in, with the class to declare the call @staticmethod def static _method (msg): Return msgprint a.class_method ("haha") print a.static_method ("haha")
Derived sub-class
Python supports multiple inheritance;
Class A (object): Author = ' Shizhen ' def __init__ (self,page): SELF.PA = page print self.pa def f (sel F,number): Print number + # Inherits Class B (A): def __init__ (self,id): #这里是调用父类的初始化方法, the new class can be super super (b , self). __init__ (id) b = B (Ten) B.F (10)
Dir and dict---view properties
Each object, each class has its own dict, which is equivalent to a namespace that holds the properties of its own class
Dir is a built-in function that can be called directly, returning properties from all objects, including properties of the parent class, returning a sequence that shows only what the attribute is;
Dict is a class-self-contained method that returns all the properties in the current class, excluding the attributes of the parent class, and returns a dictionary, including the property and its value;
Class A (object): Author = ' Shizhen ' def __init__ (self,page): SELF.PA = Page A = A (ten) #这个是a这个实例的字典, after A is created, only 10 this element, so only 10print a.__dict__# in the dictionary calls the object's own method to view the properties of the current class print A.__dict__print dir (a) print dir (a) #文档方法A. __doc__
Destructors
__DEL__: When the object's reference technique is 0, the Python interpreter automatically calls this function, and if it is an inheritance mechanism, Del releases the parent object first;
Visibility of
__650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>id represents private, only the class can be called;
Multiple inheritance
The new class adopts a breadth-first algorithm;
*attr
Class A (object): Author = ' Shizhen ' def __init__ (self,page): SELF.__PA = page def f (self): print "H Ello "A = A (ten) #这个是获取一个对象, the method is taken out for external use getattr (A, ' F ') () #判断a类中是否有这个方法print hasattr (A, ' f ') #还有delattr和setattr, delete and set , which is also the feature of dynamic language
This method applies not only to classes, but also to instances
Built-in functions
Issubclass (Sub,sup) determines if it is a subclass
Isinstance (Obj,classtype) determines whether an instance of a class
Exercises:
-
Implements a (x, y) point class that represents points on coordinates (x, y), If x, Y is not provided when constructing the point object, the default is the origin, implementing a Retangle class representing a rectangle, using four points as the parameter of the rectangle's constructor, implementing an area instance method that returns the size of the rectangle
Class point (object): def __init__ (self,x=0,y=0): self.xpoint=x self.ypoint=yclass Retangle (object): def __init__ (SELF,POINT1,POINT2,POINT3,POINT4): self.point1 = point1 self.point2 = point2 self.point3 = point3 self.point4 = point4 Def findck (SELF,POINT1,POINT2,POINT3,POINT4): self.length = abs (Point1.xpoint-point2.xpoint) self.width = abs (Point2.ypoint-point3.ypoint) def area (self): self.finDCK (SELF.POINT1,SELF.POINT2,SELF.POINT3,SELF.POINT4) self.result = self.length*self.width return Self.resultp1=point ( -1,2) p2=point (3,2) p3=point (3,-1) p4=point ( -1,-1) re = retangle (P1,P2,P3,P4) print re.area ()
2. Implement a singleton (singleton), that is, all instances of the class actually point to the same object, or it can be understood that all ID returns are the same memory address.
Class Singleton (object): #在new上做分配, guaranteed to be assigned only once space Def __new__ (CLS): If not hasattr (CLS, ' _instance '): or iginal = Super (singleton, cls) cls._instance = original.__new__ (CLS) Return cls._instances = Singleton () p = Singleton () S.page = 10print P.page
New is used to allocate space, and Init is initialized after the split is complete.
Learning Log---Python (new class, Object oriented)