Network study notes python environment epidemic under 4.8

4.8

The lesson review

  1. Programming ideas: an idea, not to go to class and object programming

    1. Process-oriented: the core of the process, the process refers to the steps to resolve the problem, the program will streamline, simplify. Poor scalability

    2. Object-oriented: the core of the object, the object is a container for containing data and functions. The program is highly integrated. Decoupling and enhance the degree programs, thus enhancing the scalability of the program. The design of complex, prone to over-design problems

      Extensibility is not the only criterion of a program is good or bad, the idea of ​​object-oriented and process each use

  2. Object-Oriented Programming

    1. Categories: integration of data and functions, is itself an object. Objects have the same properties or functions, it may be the same category

      Distinction is based on the same property attribute name, not the attribute name

    2. Objects

    3. First define the class, after calling generates an object class, as opposed to thinking of life order. Procedure call is called instantiation

  3. subject system

    Objects

    • Students Object
      • Student's name, age, class
      • Course features
    • Target Audience
      • Course name, period
  4. Call the class what happened

    1. Generates an empty object
    2. python automatically trigger init, complete initialization
    3. The object is assigned to produce the object name
  5. init Note

    1. init self must receive as the first argument
    2. There must be no return value
  6. View the object's name __dict__to get a dictionary, the dictionary can be operated with or modify access

  7. Using the target point, point-like modification

  8.  stu.name = 'deimos'
     # 若对象中有名字,则修改
     stu.xxx = 30
     # 若没有,则在对象名称空间新增
     stu.school = '111'
     # 如果在类中有school,对象中没有,则此操作是在对象中新增属性,而不是改了类属性
    
  9. Inside method of using class time to put their objects as a parameter

  10. Class by using the class of functions and data which, when used in strict accordance with the function function method, the class method which will automatically pass parameters

  11. python3 class is generated in a data type , can be seen by the class name of the object type (object)

  12. Variables defined in the class data class attribute, is shared by all the objects, pointing to the same memory address

    print(id(Student.school)) # 4301108704
    print(id(stu1.school)) # 4301108704
    print(id(stu2.school)) # 4301108704
    print(id(stu3.school)) # 4301108704
    
  13. All points are the same functional object, but the object is bound to different different binding methods, memory addresses are not the same

    print(id(Student.choose)) # 4335426280
    print(id(stu1.choose)) # 4300433608
    print(id(stu2.choose)) # 4300433608
    print(id(stu3.choose)) # 4300433608
    

operation

analysis

  • A course corresponds to a class, a class corresponding to a campus
class School:
    # 公共数据:学校名,关联班级
    school_name = 'zjnu'


    def __init__(self, nickname, addr):
        self.addr = addr
        self.nickname = nickname
        self.classes = []  # 往列表里加值,代表新建班级


    def related(self, class_name):
        # 关联班级
        self.classes.append(class_name)


    def show_class(self):
        for class_name in self.classes:
            print(self.school_name,self.addr,self.nickname,class_name)


school_obj1 = School('数计学院', '20幢')
school_obj2 = School('音乐学院', '18幢')

school_obj1.related('14班')
school_obj2.related('15班')
school_obj1.show_class()
school_obj2.show_class()

class Class:
    def __init__(self, name):
        self.name = name
        self.course = None

    def related_course(self, course):
        self.course = course


class_obj1 = Class('14期')
class_obj2 = Class('13班')
class_obj1.related_course('python')
class_obj2.related_course('linux')

demand

After class built schools and classes, school classes want to view courses: the name is not operated, the operation target, so related methods associated with the object

modify:

class School:
	def related_class(self,class_obj):
		self.classes.append(class_obj)
        # 原来传的是班级的名字,现在改传班级对象
		
	def tell_class(self):
		print(self.nickname)
		for class_obj in self.classes:
            class_obj.show_info()
            # 得到对象,可以使用对象下的方法
            
class Class:
    def __init__(self, name):
        self.name = name
        self.course = None

    def related_course(self, course):
        self.course = course

    def show_info(self):
        print(self.__dict__)
class Course:
	def __init__(self,name,period,price):
        self.name = name
        self.period = period
        self.price = price
    
    def tell_info(self):
        print(self.name,self.period,self.price)
        
course_obj1 = Course('python开发','6mon','2000')

to sum up

In the definition of the class, when using the data associated with, the referenced object, not a reference name, so he method of the associated class object can be referenced in the class

As long as the object passed in the past, and consequently do have

Requirements: Use pickle, serialized object files saved to pkl

Make the filename unique, with uuid module, the init, the initialization of each object to the object when a uuid, as an object id

import uuid
uuid.uuid4()

Package

Object-oriented package is a characteristic of the core, the integration means that the package can be processed further class attribute, an attribute of the package hiding operation

Packaging class attributes

In class attributes, functions and data, preceded by __possible hidden attribute

class Foo:
	__x = 1
	def __f1(self):
		print('from f1')
    def

Foo.f1
# 报错,被隐藏,不能直接访问到了
print(Foo.__dict__)
# _Foo__f1,_Foo__x
  • This is a hidden attribute modification operation can not directly access to the properties of the class, but still be able to _类名__属性名access to

  • Foreign effectively hidden, normal access from within the class

  • Will underscore the hidden attribute unified deformed stage the class definition syntax check , will not deformation properties beginning with underscore after checking grammar

  • In the initialization phase, the init Properties Hide

    def __init__(self,name,age):
    	self.__name = name
    	self.__age = age
    

Why hide

The purpose of the definition is to use, not directly with the outside world after the hidden properties of the class, only to let the outside world by using the specified mode: provides an interface

def __init__(self,name,age):
	self.__name = name
	self.__age = age

def get_name(self):
	print(self.__name)
# get_name就是一个接口,只能通过接口访问到属性,可以在接口内添加其他逻辑,控制使用者对属性的操作
def set_name(self,new_name):
    if not isinstance(new_name,str):
        print('修改非法')
        return
    self.__name = new_name
    # 提供了一个修改名字的接口,判断
    

Guess you like

Origin www.cnblogs.com/telecasterfanclub/p/12661579.html