Briefly describe the concept of object-oriented programming in Python.

Source: Internet
Author: User

Briefly describe the concept of object-oriented programming in Python.

Object Oriented Programming (OOP) is a Programming idea. OOP regards objects as the basic unit of a program. An object contains functions for data and operation data.

Process-oriented programming treats a computer program as a collection of commands, that is, the sequential execution of a group of functions. To simplify the program design, process-oriented functions are further split into subfunctions, that is, large functions are cut into small functions to reduce the complexity of the system.

Object-Oriented Programming treats computer programs as a collection of objects, and each object can receive and process messages sent from other objects, the execution of a computer program is the transmission of a series of messages between objects.

In Python, all data types can be regarded as objects, and you can also customize objects. The custom object data type is the concept of Class in the object-oriented model.

Let's take an example to illustrate the differences between process-oriented and object-oriented processes.

Suppose we want to process the student's score table. To represent a student's score, the process-oriented program can be represented by a dict:

std1 = { 'name': 'Michael', 'score': 98 }std2 = { 'name': 'Bob', 'score': 81 }

The processing of students' scores can be implemented through functions, such as printing students' scores:

def print_score(std):  print '%s: %s' % (std['name'], std['score'])

If we adopt the object-oriented programming philosophy, we should consider not the execution process of the program, but the data type Student which should be regarded as an object, this object has the name and score attributes ). To print a student's score, you must first create the student's corresponding object, and then send a print_score message to the object so that the object can print its own data.

class Student(object):  def __init__(self, name, score):    self.name = name    self.score = score  def print_score(self):    print '%s: %s' % (self.name, self.score)

Sending messages to an object is actually calling the association function corresponding to the object, which is called the Method of the object ). The object-oriented program is written like this:

bart = Student('Bart Simpson', 59)lisa = Student('Lisa Simpson', 87)bart.print_score()lisa.print_score()

Object-Oriented Design comes from nature, because in nature, the concepts of classes and instances are natural. Class is an abstract concept. For example, Student, the Class we define, refers to the Student concept, and Instance is a specific Student, for example, bart Simpson and Lisa Simpson are two specific Student:

Therefore, the object-oriented design idea is to abstract the Class and create an Instance based on the Class.

Object-oriented abstraction is higher than functions, because a Class contains both data and operation data.
Summary

Data encapsulation, inheritance, and polymorphism are three main characteristics of object-oriented, which will be explained in detail later.

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.