Classes and objects
process-oriented programming:
advantages: greatly reduces the complexity of the program
Span style= "font-family:"microsoft yahei"; Font-size:14px "> Cons: a set of pipeline or process is used to solve a problem, the production line of soda can not produce a car, even if it can, is also big change, change a component, reaching
Object-oriented Programming
advantages: It solves the extensibility of the program, A single modification of an object is immediately reflected in the entire system, such as the ability to change features and skills of a character parameter in the game
Cons: Controllability is poor, not like the process-oriented programming pipeline can be very accurate prediction problem processing flow and results, object-oriented program once started by the object
everything in Python is object, and Python3 unifies the concept of classes and types, which are classes. >>> Dict#type dict is Class Dict<class 'Dict'>>>> d = dict (name='Egon')#Instantiate D>>> D.pop ('name')#send a message to D, execute the method of D'Egon'A class is a combination of a feature (the definition of a variable) that is common to a series of objects and a skill (the definition of a function).classChinese:country=' China' #chinese.__init__ (P1, ' Egon ', ' + ', ' male ') def __init__(self,name,age,sex):#P1. Name=name;p1. Age=age,p1. Sex=sexSelf. Name=name self. Age=Age self . Sex=SexdefTalk (self):Print('Talking', self) property referencePrint(chinese.country)Print(Chinese.talk) Chinese.talk (123) chinese.x=1Print(chinese.x) chinese.country=123123123123123Print(chinese.country)#instantiation ofclassChinese:country=' China' #chinese.__init__ (P1, ' Egon ', ' + ', ' male ') def __init__(self, name, age, Sex):#P1. Name=name;p1. Age=age,p1. Sex=sexSelf. Name =name self. Age=Age self . Sex=SexdefTalk (self):Print('%s is talking'%Self . Name) P1=chinese ('Egon',' -','male')#chinese.__init__ (P1, ' Egon ', ' + ', ' male ')P2=chinese ('Alex','9000','female')#chinese.__init__ (P1, ' Egon ', ' + ', ' male ')Object use: Only one, property referencePrint(P1. Name)Print(P1. Age)Print(P1. SEX)Print(P2. Name)Print(p1.country)Print(P2.country) P1.talk () P2.talk ()classChinese:country=' China' def __init__(self, name, age, Sex):#self.country=123123123123123123Self. Name =name self. Age=Age self . Sex=SexdefTalk (self):Print('%s is talking'%Self . Name) to view the class namespacePrint(Chinese.__dict__) The spatial P1 of the object=chinese ('Egon',' -','male')#chinese.__init__ (P1, ' Egon ', ' + ', ' male ')P2=chinese ('Alex',' the','male')Print(P1.__dict__)Print(P1. Age)#p1.__dict__[' age ']Print(P1.country,id (p1.country))Print(P2.country,id (p2.country))Print(Chinese.talk)Print(P1.talk) p1.talk ()#Chines.talk (p1)Print(P2.talk) p2.talk ()#Chinese.talk (p2)
Python Automation Development class note "DAY06"-Python Advanced (Class)