Pickle Serialization Supplement
# Pickle Load # There are now two Python programs that call relationships as follows # s1.py class foo: = foo (x, y) pickle.dump (obj, open (' db ', ' WB ')) s2.py# s2.pyfromImport foopickle.load (db)# s2.py to deserialize the containing object, be sure to import the object's corresponding class, otherwise pickle load will error
The inheritance relationship of a class multiple inheritance relationship has a common parent class as follows
If we have two inheritance (or multiple inheritance)
Let's say we're going to call a method:
The ①f class will go from left to right to look for the parent class and find Class D
②d class does not have this method, continue to find in Class D parent class, find Class B
The ③b class also does not have this method, at this time, Python will not go directly to inherit the common parent Class A to find, but return to the E-Class lookup, the Class E also does not have this method, continue in class E of the parent class C lookup
The ④c class also does not have this method, when Python goes back to determine if you have any of the remaining Class F classes,
⑤ now not, then go into the common parent Class A-class lookup
No common parent class
There is no common parent case, please refer to the Common parent class
Object-oriented: polymorphic
#because Python itself is polymorphic,#so here's a python syntax for describing Java polymorphic representationsclassint:Passclassfloat (int):#inherits the Int class PassclassSTR (int):#inherits the Int class Pass#I created three classes, when I defined a functiondeffunc (int arg):#Java must specify the type of the incoming parameter Print(ARG)#at this point the polymorphic performance is,Obj1 =float () func (obj1) obj2=str () func (obj2) obj=Int () func (obj)#The arg parameter receives a subclass of the int class or int, which is the polymorphic
Object-Oriented Members: Fields, Methods, property classes: Fields
#static fields: Country saved in class#dynamic field: Self.name saved in the called ObjectclassProvince:country='China' def __init__(self,name): Self.name=Namehn= Province ('Henan') HB= Province ('Hebei') SD= Province ('Shandong') DB= Province ('Heilongjiang')#in general: Access your own fields#Rules:#dynamic fields can only be accessed with objects#static fields are accessed with classes (preferably not with object access)HN = Province ('Henan')Print(Hn.name)#Object access dynamic fieldPrint(Province.country)#class Access static fieldsPrint(Hn.country)#Object access static field (not recommended)
Methods in the Python class
#three ways of Python class#1. Common method: At least one self, object execution#2. Static method: Arbitrary parameter class Execution (can object execution, not recommended)#3. Class method: At least one CLS, class execution (can object execution, not recommended)classProvince:country='China' def __init__(self,name): Self.name=name#normal method, called by the object to execute (method belongs to Class) defShow (self):Print(Self.name)#static methods, called by the class to execute (when the method does not need to encapsulate the value inside the object, the method can be Ctrip static method)@staticmethoddefF1 ():Print('Test') #class Method@classmethoddefF2 (CLS):#CLS # class name #CLS () # Object Print(CLS)#static methods that use class invocationprovince.f1 ()#invoking a normal class method with an objectobj = Province ('111111') obj.show ()
Property
#the first way that properties are usedclassPager:def __init__(self, all_count): Self.all_count=All_count @propertydefAll_pager (self):returnself.all_count @all_pager. SetterdefAll_pager (self,value): Self.all_count=value @all_pager. deleterdefAll_pager (self):delSelf.all_count#modifications and deletions in properties#The top is to field the method, but the field can be modified,P= Pager (110) P.all_pager= 123#when I go to modify it, it will execute the function under the @all_pager.setter decorator (itself, the Decorator guide, and will not help you to implement the modification function)Print(P.all_pager)#has now been modifieddelP.all_pager#Similarly, Del will find the class @all_pager. The All_pager method under the deleter adorner also does not implement the delete function#P.all_pager # because it has been deleted, so the call will be an error
#the second way that a property existsclassPager:def __init__(self, all_count): Self.all_count=All_countdefF1 (self):returnSelf.all_countdefF2 (self,value): Self.all_count=valuedefF3 (self):delself.all_count Foo= Property (Fget=f1, Fset=f2, fdel=F3) P= Pager (101) Result= P.foo#Call FgetPrint(Result) P.foo= 123#Set FsetPrint(P.foo)delP.foo#Delete Fdel
Member modifiers
#member Modifiers#Private:#only the class itself (Private Dynamic field, static field, private static method, normal method, class method, private property)#Public:#can be accessed outside the class and can be accessed by the public#PS: Not the last resort, do not force access to private member _ class name __ Private XXXclassFoo:__CC='Test' #private static fields def __init__(self,name): Self.__name= Name#Private dynamic Fields@staticmethoddef __f1():#private static Methods return 'private static' def __f2(self):#Common methods of private return 'Private Common Method'@classmethoddef __f3(CLS):#Private class Methods returnCls'Private class Methods'@propertydef __f4(self):return 'Private Properties' defF10 (self):Print(Foo.)__f1())#calling private static methods Print(self.)__f2())#calling private common methods Print(Foo.)__f3())#calling a private class method Print(self.)__f4)#Calling private propertiesobj= Foo ('Eason') OBJ.F10 ()
Special method, construction method, and destructor method
#construction method, destructor method, special methodclassFoo:#constructs the method when the object is called (Example foo ()) def __init__(self,name,age): Self.name=name Self.age= Age#destructor, the method that is executed before the memory garbage collection def __del__(self):Pass #Special Methods #Call , the method to execute when the object is invoked def __call__(self):Print('Pager') #str, changing the default object return value def __str__(self):return '%s-%d'%(Self.name, self.age) obj= Foo ('Jack', 18)#Construction Method ExecutionObj ()#Call MethodFoo ('Eason', 24) ()#class calls the Call methodobj1= Foo ('Alex', 55)Print(OBJ1)#default No __str__ method, will return memory address, there is __str__ method to execute __str__ method#gets the data encapsulated in the objectret = obj1.__dict__Print(ret)
Python Learning notes on-day8 (Pickle additions, fields, object-oriented members, member modifiers, special methods)