Python language programming 1

Source: Internet
Author: User

Object-Oriented Programming

Object-oriented programming--object oriented programming, short for OOP, is a programming idea. OOP takes objects as the basic unit of a program, and an object contains functions for data and manipulating data.

Process-oriented programming treats a computer program as a set of commands, which is the sequential execution of a set of functions. In order to simplify the program design, the process will continue to cut the function into sub-function, that is, the large function by cutting into small block function to reduce the complexity of the system.

Object-oriented programming treats a computer program as a set of objects, and each object can receive messages from other objects and process them, and the execution of a computer program is a series of messages passing between objects.

In Python, all data types can be treated as objects and, of course, objects can be customized. The custom object data type is the concept of class in object-oriented.

We use an example to illustrate the differences between process-oriented and object-oriented programming processes.

Suppose we want to deal with the student's score table, in order to indicate a student's achievement, the process-oriented program can be represented by a dict:

The process of student performance can be achieved through functions such as printing students ' grades:

If we adopt the object-oriented program design idea, we prefer not to think about the program's execution flow, but this Student data type should be treated as an object, which owns the object name and both score properties. If you want to print a student's score, you first have to create the corresponding object for the student, and then send a message to the object to print_score let the object print its own data.

Sending a message to an object is actually a call to the object's corresponding associated function, which we call the object method. The object-oriented program is written like this:

The idea of object-oriented design is from the nature, because in nature, the concept of classes (class) and instances (Instance) is very natural. Class is an abstract concept, such as our definition of class--student, which refers to the concept of a student, while an instance (Instance) is a specific Student, for example, Bart Simpson and Lisa Simpson is a two-specific student:

Therefore, the object-oriented design idea is to abstract the class, according to the class to create instance.

Object-oriented abstraction is also higher than a function, because a class contains both data and methods to manipulate the data.

Exception handling

You can use the Try/except statement to catch an exception.

The try/except statement is used to detect errors in a try statement block, allowing the except statement to catch exception information and handle it.

If you do not want to end your program when an exception occurs, simply capture it in a try.

Grammar:

The following is a simple syntax for try....except...else :

Try works by starting a try statement, and Python is tagged in the context of the current program so that when an exception occurs, it can go back here, the TRY clause executes first, and what happens next depends on whether an exception occurs at execution time.

    • If an exception occurs when the statement after the try is executed, Python jumps back to the try and executes the first except clause that matches the exception, and the control flow passes through the entire try statement (unless a new exception is thrown when the exception is handled).
    • If an exception occurs in the statement after the try, but there is no matching except clause, the exception will be submitted to the upper try, or to the top of the program (This will end the program and print the default error message).
    • If no exception occurs when the TRY clause executes, Python executes the statement after the Else statement (if there is else), and then the control flow passes through the entire try statement.
Instance

Here is a simple example that opens a file where the contents of the file are written and the exception does not occur:

The above program output results:

Python language programming 1

Related Article

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.