The process-oriented and object-oriented differences of Python

Source: Internet
Author: User

First, process-oriented

 1, the process : The core is the process of two words, process refers to the steps to solve the problem, such as the design of a pipeline, is a mechanical way of thinking.

is the program from the top to the next step, step by step from top to bottom, to solve the problem from beginning to end. The basic design idea is that the program starts with solving a big problem, and then breaks down a big problem into many small problems or sub-processes, and the process continues to decompose until the small problem is simple enough to be solved within a small step.

 2. Advantages and Disadvantages :

      Advantages: The complexity of the problem process, and then simplify.

Cons: Poor scalability

  3. Example: process-oriented Login registration program

ImportJson,redeflogin ():" "user input: return:" "Usename= Input ('User name:'). Strip () PWD= Input ('Password:'). Strip () Tel= Input ('Tel:'). Strip () Mail= Input ('Email:'). Strip ()return {        'Usename': Usename,'pwd':p WD,'Tel': Tel,'Mail': Mail}defAuthentication (use_info):" "determine if information such as user name and password is legal: return:" "Is_valid= True#Default Legal    ifLen (use_info['Usename'])==0:Print('the user name cannot be empty! ') Is_valid=FalseifLen (use_info['pwd']) < 6:        Print('password length must not be less than 6 bit! ') Is_valid=FalseifLen (Re.findall ('1[0-9]{10}', use_info['Tel']))==0:Print('phone format is wrong! ') Is_valid=Falseif  notRe.search (R'@.*?.com$', use_info['Mail']). Group ():#using the python r prefix, you do not have to consider escaping the problem, ' * ' Add a, you can let '. ' Use a non-greedy match        Print('Incorrect mailbox format') Is_valid=Falsereturn {        'valid': Is_valid,'Info': Use_info}defRegister (auth):" "If the input information is valid, register, write to the file or database:p Aram Auth:: return:" "    ifauth['valid']==True:with Open ('Data.json','W', encoding='Utf-8') as F:json.dump (auth['Info'],f)defMain ():" "Main Logic program: return:" "Use_info=login () auth=Authentication (use_info) register (auth)if __name__=='__main__':#execute the following program directly when called without executingMain ()
View Code

Note: It is generally thought that if you just write some simple scripts to do some one-time tasks, it is great to use a process-oriented approach, but if the task you are dealing with is complex and needs to be constantly iterated and maintained, it is most convenient to use object-oriented.

Second, object-oriented

 1, object-oriented: The core is the object of two words, features and skills of the combination of the body.

 2. Advantages and Disadvantages:

Advantages: High Scalability

Cons: High complexity of programming

  3, the application scenario : The user needs frequently change, the Internet application, the game, the enterprise application and so on.

 4, about the object-oriented several noun explanation

   class: A class is a combination of features and skills similar to a series of objects, like a template. The properties of these objects are defined in the class, together with the common method.

   attributes: human beings contain many characteristics, which are described by programs, called attributes, such as age, height, gender, name, etc. are called attributes, and a class can have multiple attributes.

   methods: Human beings not only have height, age, sex these attributes, but also can do a lot of things, such as talking, walking, eating and so on, compared to the attribute is a noun, talking, walking is a verb, these verbs are described by the procedure is called method.

    instance (object): An object is an instantiated instance of a class, a class must be instantiated before it can be called in a program, a class can instantiate multiple objects, each object can also have different properties, like human refers to everyone, each person refers to the specific object, people and people before there is common , there are also differences.

    instantiation: The process of turning a class into an object is called instantiation.

  5. Simple example

#objects in the real world:" "Object 1 Characteristics Occupation: Student name: Wang er Sex: male Age: 22 Skill Learning playing Game Object 2 characteristic occupation: Student        Name: Zhang San Sex: Male Age: 20 skill Learning playing game reading object 3 characteristics Occupation: Student name: Wan Wan Sex: Female Age: 18 Learn to play the game read the real class: Characteristics: Occupation: Student Skills: Learn to play the game reading in the program:" "#First ClassclassSchool_learn:job='Student'    defStudy (self):Print('study hard!')    defPlay_game (self):Print('play games as little as can')    defRead (self):Print('read books more and more')#call the class to produce the object againStudent1 =School_learn () Student2=School_learn () Student3=School_learn ()Print(STUDENT1)#<__main__. School_learn object at 0x000002b70f8d7588>Print(Student2)Print(STUDENT3)

 6, object-oriented three major features

<1> Encapsulation (encapsulation): Assignment of data in a class, internal invocation is transparent to external users, which makes the class into a capsule or container, with the data and methods of the class in the bread.

<2> inheritance (inheritance): A class can derive a subclass, in which the properties, methods defined in the parent class are automatically inherited by the quilt class.

<3> Polymorphism (polymorphism): Polymorphism is an important object-oriented feature, simple point: "An interface, a variety of implementations", refers to a base class derived from a different subclass, And each subclass inherits the same method name and implements the method of the parent class differently, which is the various forms that the same thing shows.

Note: Here is a brief description of the next few major features, which we will delve into later.

  

  
 
 

The process-oriented and object-oriented differences of Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.