python's object-oriented basis (defined class, create objects, namespace)

Chapter VII of the object-oriented

1, process-oriented programming core is the "process" the word refers to the process steps to resolve the problem, which is to do and then to do based on the idea to write programs like writing an assembly line, is a mechanical way of thinking

Advantages: the process of complex issues, and further simplification of disadvantages: poor scalability

2, the object-oriented core "subject" word object refers to the characteristic (variable) and skills (function) of the combination, it is necessary to create an object of the present one specific execution of the program is done directly by the object interactions

Advantages: scalability drawbacks: complexity of the above process-oriented programming

7.1 Class

7.11 define a class

1, class: The object is a combination of characteristics and skills, the kind that is similar to a series of objects and features a combination of skills

2, in the real world: we must first have an object, and later with the development of human civilization summed up the class, the object is a concrete existence, while the class is just an abstract concept

3, in the program, be sure that: first define the class, after calling the class to produce an object

class OldboyStudent: 
    
    School = " Oldboy "                       # objects have similar characteristics 
   
    DEF Learn (Self):                      # objects have similar skills 
        Print ( ' IS Learning ... ' )

 

Note: The definition phase will be executed immediately in the class class body of code will produce the namespace of the class, the class name of the implementation body of code generated in the process space to store the name of the class

How to view:

Print (OldboyStudent. the __dict__ )                # view namespace class, the dictionary is displayed in the form of 
Print (OldboyStudent. the __dict__ [ ' School ' ])       # Oldboy Print (OldboyStudent.school)                  # Oldboy Print (OldboyStudent. the __dict__ [ ' Learn ' ] )        # <function OldboyStudent.learn AT 0x00000146F735BD90> Print (OldboyStudent.learn)                   # <function OldboyStudent.learn AT 0x00000146F735BD90>




OldboyStudent.__dict__['learn'](123)         # is learning...
OldboyStudent.learn('xxx')                  # is learning...
OldboyStudent.country='China'               #
OldboyStudent.school='偶的博爱'              #
del OldboyStudent.country                   #
print(OldboyStudent.__dict__)

7.12 calls produce an object class

class OldboyStudent: 
    School = " Oldboy "                  # with variable represents wherein 
                                 
    DEF  the __init__ (Self, name, Age, Sex): # Self STU1 = name = "Ma Dongmei" = 18 is Sex Age = "FEMALE" 
        the self.name name =               # STU1. name = "Ma Dongmei" 
        self.age Age =                 # stu1.age = 18 is 
        self.sex Sex =                 # stu1.sex = "FEMALE" 
  
    DEF Learn (Self):                 #   a function represented skills 
        Print ( ' IS ... Learning ' , Self) 
    DEF choose(self):
        print('choose course...')

What do call the class happened:

1, first generates an empty object STU1 2, inside the class will automatically trigger the function 3, and then the empty object with the parameter in parentheses STU1 the call classes (stu1, "Ma Dongmei", 18, 'female') , these with four parameters passed to the function __init__ __init__

Initialization process of creating an object, the object feature set

STU1 = OldboyStudent ( " MA Dong-mei " , 18 is, ' FEMALE ' )   # OldboyStudent .__ the init __ (STU1, "Ma Dongmei", 18 is, 'FEMALE') 
STU2 = OldboyStudent ( " sweet " , 21 is, ' MALE ' )     # OldboyStudent .__ init__ (stu2, "sweet", 21 is, 'MALE') 
stu3 = OldboyStudent ( " rough opening " , 22 is, ' MALE ' )
 Print (stu1.name, stu1.age, stu1.sex)
 Print (stu2.name, STU2 .age, stu2.sex)
 Print (stu3.name, stu3.age, stu3.sex)

After the initialization process of creating an object, the object feature set

stu1=OldboyStudent()
stu1.NAME='马冬梅'
stu1.AGE=18
stu1.SEX="female"print(stu1.NAME,stu1.school)

--- call the class "of the object class is generated, the subject may also be referred to as an instance of the class, also known as procedure calls class class instantiation

7.13 class name space objects and namespaces

#school='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
class OldboyStudent:
    #school='oldboy'
    name='axaxaxaxxaaxs'
    def __init__(self,name,age,sex):
        self.name=name
        self.age=age
        self.sex=sex    
stu1=OldboyStudent('李三胖',18,'male') #OldboyStudent.__init__(stu1,'李三胖',18,'male')
print(stu1.name)                      ## Li three priority own fat when namespaces and classes stu1 target namespace has name          
Print (stu1.school)                     # When no object name space stu1 school, the class namespace lookup Oldboy 
                                    # namespaces do not like when the error

Find the order of object properties: first find the object's own namespace ---- "class namespace

Special about 7.14 binding method

class OldboyStudent:
    def learn(self):
        print('%s is learning' %self.name)

1. within a class variable is shared for all objects, all objects point to the same memory address is

print(id(stu1.school))          #   2143429964384
print(id(stu2.school))          #   2143429964384
print(id(OldboyStudent.school))  #   2143429964384

2. The function within a class, the class may be used, but when a class is a general function , function has several parameters common to pass several parameters

print(OldboyStudent.learn)      #<function OldboyStudent.learn at 0x000001E46CA5CF28>
OldboyStudent.learn(stu1)       #李三胖 is learning

3. The internal class defined functions, in fact, is to use an object, and it is binding to the target by binding to different objects is different binding methods, memory addresses are not the same, but in fact refer to the same function

print(stu1.learn)#<bound method OldboyStudent.learn of <__main__.OldboyStudent object at 0x000001FC28E2BB38>>
print(stu2.learn)#<bound method OldboyStudent.learn of <__main__.OldboyStudent object at 0x000001FC28E2BB70>>

Binding method special is that :

1, binding to whoever who should call 2, who will call the Who as the first argument, but there may be other parameters

stu1.learn()        # OldboyStudent.learn(stu1)         #李三胖 is learning
stu2.learn()        # OldboyStudent.learn(stu2)         #王大炮 is learning

7.15 Creating object counters

class Foo:
    n=0
    def __init__(self):
        Foo.n+=1                # 不要写成 self.n+=1
obj1=Foo()
obj2=Foo()
obj3=Foo()
print(obj1.__dict__)        # {}
print(obj2.__dict__)        # {}
print(obj3.__dict__)        # {}
print(obj1.n)               # 3
print(obj2.n)               # 3
print(obj3.n)               # 3
View Code

Guess you like

Origin www.cnblogs.com/mylu/p/11104917.html