Process oriented programming and object oriented programming

Programming essence:  
  • Define a series of data, define a series of functions, and use functions to operate data to achieve people's desired requirements
 
1 process oriented programming
 
definition:
    Aiming at the problem/Functions to be implemented,List the specific solution steps first,Then write the specific content
    Assembly line of similar factory,Each process completes a function,There is a logical relationship between before and after.
 
advantage:
    Clear logic,Easy to understand
 defect:
    A block of code to be changed later,Then it will affect the whole body,It is likely that some changes will be made to multiple functional modules(Poor scalability)

 

example
import json
"""
 Case of poor expansibility of process oriented programming <User registration system>
    The following are encapsulated into functions
    1 Enter user information
    2 Verification information(For example convenience:Here, only check whether the user input is empty)
    3 register
    4 Three functions are sealed into one function,When this function is called, registration is performed
"""
# reflection:  How are the function return values organized in this example,Why??
# step1
def interinfo():
    username = input('username>>>:').strip()
    password = input('password>>>:').strip()
    return {'username': username, 'password': password}
# step2
def check_interfo(interinfo_return):
    is_register = True
 
 
    # # Writing method 1
    # if len(interinfo_return.get('username')) == 0:
    #     print('User name cannot be empty')
    #     is_register = False
    # if len(interinfo_return.get('password')) == 0:
    #     print('Password cannot be empty')
    #     is_register = False
 
 
    # Writing method 2
    if len(interinfo_return.get('username')) != 0 and len(interinfo_return.get('password')) != 0:
        pass
    else:
        print('The password or user name is blank')
        is_register = False
    return {'is_register': is_register, 'interinfo_return': interinfo_return}
# step3
def register(check_info_return):
    if check_info_return.get('is_register'):
        import json
        with open(r'a.txt', 'w', encoding='utf-8') as f:  # be careful:a.txt Is a relative path
            json.dump(check_info_return.get('interinfo_return'), f)
# step4
def start():
    interinfo_return = interinfo()
    check_info_return = check_interfo(interinfo_return)
    register(check_info_return)
 
 
if __name__ == '__main__':
    start()

 

 
2 object oriented programming
 
Definition: basic ideas
            Integrate the relevant data and functions used by the program into the object, and then use it
 
3 types and objects
 
  • definition:
    • Object: a container for storing data and functions
      • Integrate the originally scattered relevant data and functions into the object
    • Class: a container for storing the same data and functions of multiple objects
      • Class meaning: save space, reduce code redundancy mechanism, object-oriented programming core use pair

 

"""
 
1 define a class
2 generate object / instantiate
     Definition: the process of calling a class is instantiation   # Each time you instantiate a Student, you get a Student object
     Format: object name to be generated = class name (attribute name)
3 (add / change / delete) object attributes (add, modify and delete class attributes in the same way)
     format
     Add:
         Abbreviation: object name. New attribute name = attribute value   # Equivalent to >: object name sentence dotted double lower dict = attribute value
     Change:
         Abbreviation: object name. Property name = new property value   # Equivalent to >: object name sentence dotted double lower dict = new attribute value
     Delete format:
         del object name. Property name
4 how to view object attributes < internal data of objects is organized by dictionaries >
     Format:
         1: Print (object name. Attribute name)
         2: Dictionary value method
            >: Print (object name. _dict_. Get ('property name '))
5 object class, calling properties in class
     Format:
         Object:
         Attribute value in calling class: object name / class name. Attribute value
         Method in calling class: object name / class name
six     Attributes and search order:
     Class properties:
         The variables defined in the class are the data attributes of the class, which are shared with all objects and point to the same memory address;
         The defined function is a function property of the class
     Object attribute: unique attribute in object namespace, similar attribute in class
     Search order:
         When an object accesses a property, it first starts from itself__ dict__ If not found, go to class__ dict__ Find in
 
 
"""
 

Detailed understanding

# # 1 define a class
# class Student():  # Class name: hump body
#     school = 'sh'  #  'school': 'bj'
#
#     def __init__(self, name, age, course):
#         self.name = name
#         self.age = age
#         self.course = []
#         # Return cannot have a function return value in a class
#     def choose_course(self, course):
#         self.course.append(course)
#         print('%s Course selection%s' % (self.name, self.course))
 
 
 
 
"""
2 Generate object/instantiation 
    definition:The process of calling a class is instantiation
    format: Object name to generate = Class name(Attribute name)
"""
# stu1 = Student('jack', 23, 'art')  # Each time you instantiate a Student, you get a Student object
# stu2 = Student()
 
 
"""
3 (increase/change/delete)Object properties(The method of adding, modifying and deleting class attributes is the same)
    format
    increase:
        Abbreviation: Object name.New attribute name = Attribute value  # Equivalent to >: object name sentence dotted double lower dict = attribute value
    change:
        Abbreviation: Object name.Attribute name = New attribute value  # Equivalent to >: object name sentence dotted double lower dict = new attribute value
    Delete format:
        del Object name.Attribute name
"""
# change
# stu1.name = 'tom'  # stu1.__dict__['name'] = 'tom'
# increase
# stu1.hobby = 'run'  # stu1.__dict__['name'] = 'tom'
# print(stu1.__dict__.get('hobby'))  # run
# Delete
# del stu1.hobby
# print(stu1.__dict__.get('hobby'))
 
 
"""
 
 
4 How to view object properties<Object internal data is organized in a dictionary>
    format:
        1: print(Object name.Attribute name)  
        2: Dictionary value method
            >: print(Object name.__dict__.get('Attribute name'))
"""
# print(stu1.name)
# print(stu1.__dict__)
# print(stu1.__dict__.get('name'))
 
 
"""
5 object,Class calls data and function properties in a class
"""
# 1 Call data properties
# print(stu1.school)
# print(Student.school)
# 2 Calling function properties
# stu1.choose_course('art')
# Student.choose_course(stu1, 'art')
 

 

Keywords: OOP

Added by Calcartman on Thu, 02 Dec 2021 22:30:54 +0200