Python Object-oriented introduction

Source: Internet
Author: User

Data structure Python's data structures are list dictionaries that use them to efficiently store data

Algorithm writer program Flow programming logic

    • Process oriented: Write base code from top to bottom according to business logic
    • Function: A function code is encapsulated in a function, it will not need to be repeated later, only the function can be called
    • Object-oriented: classify and encapsulate functions to make development "faster, better and stronger ..."

Programming Paradigm

Programming is a programmer with a specific syntax + data structure + algorithm composed of code to tell the computer how to perform the task of the process, a program is a programmer in order to get a task result and a set of instructions, is so-called all roads to Rome, the way to achieve a task there are many different ways, The types of programming that are summed up by the characteristics of these different programming methods are the programming paradigm.

Different programming paradigms essentially represent different solutions to various types of tasks, and most languages support only one programming paradigm, and of course some languages can support multiple programming paradigms at the same time.

Two of the most important programming paradigms are process-oriented programming and Object-oriented programming.

Process-oriented programming (procedural programming)
Procedural programming uses a list of instructions to tell the computer what to do step-by-step.
Process-oriented programming dependencies-you guessed-procedures, a procedure contains a set of steps to be computed, and the process is called Top-down languages, which is the procedure 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 range.

So we generally think that if you just write some simple scripts to do some one-off tasks, it's great to use a process-oriented approach, but if the task you're dealing with is complex and needs to be constantly iterated and maintained, it's most convenient to use object-oriented.

Object-oriented programming (object oriented programing)

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.

Why use object-oriented?
    1. Make your programs more scalable and easy to change, making development more efficient
    2. An object-oriented program can make it easier for people to understand your code logic and make team development easier.
Object Oriented Introduction

You will encounter some nouns in the process of learning object-oriented, let us first explain

noun explanation

class : A class is an abstraction, Blueprint, prototype, template for a class of objects that have the same properties. The properties of these objects are defined in the class (variables (data)), common methods

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 properties

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, and each object can have different properties, just as human refers to everyone, each person refers to the specific object, people and people before there is common, There are also different

For example: People, cats, dogs are all an object

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

Object-oriented 3 major features

Encapsulation Package

The assignment of data in a class, internal invocation is transparent to external users, which makes the class A capsule or container in which the data and methods of the class are contained.

Like a human being inside

1. Prevent data from being arbitrarily modified
2. So that external programs do not need to focus on the internal structure of the object, only through this object
Provide direct access to external interfaces.

Inheritance inheritance

A class can derive subclasses, properties, methods defined in the parent class, automatic quilt class inheritance

Polymorphism Polymorphism

Polymorphism is an important feature of object-oriented, simple point: "An interface, a variety of implementations," refers to a base class (the parent class ) derived from a different subclass, and each subclass inherits the same method name at the same time the parent class method to do a different implementation, this is the same thing shows a variety of patterns.

Programming is actually a process of abstracting the concrete world, which is a manifestation of abstraction, abstracting the common denominator of a series of specific things, and then through this abstract thing, and dialogue with different concrete things.

Sending the same message to different classes of objects will have different behavior.

For example, if your boss lets all employees start working at nine o'clock, he says "get started" at nine o'clock, instead of saying to the salesperson: "Start a sales job," say to the technician, "Start technical work", because "employee" is an abstract thing, so long as the employee can start to work, He knows that. As for each employee, of course, they do their job and do their jobs.

Polymorphism allows the object of a subclass to be used as an object of the parent class, a reference to a parent type to an object of its subtype, and the method called is the method of that subtype. The code that references and invokes the method is already determined before it is compiled, and the object pointed to by the reference can be dynamically bound during run time

Object-oriented programming is a way of programming, which requires " classes " and " objects " to be implemented, so object-oriented programming is actually the use of "classes" and "objects".

A class is a template that can contain multiple functions and functions in a template.

  A class must be instantiated before it can be called in a program

Objects are instances created from templates that can execute functions in a class through an instance object

No matter what the form of programming, we have to clearly remember the following principles:
    1. Writing duplicate code is a very bad low-level behavior.
    2. The code you write needs to change frequently.

    • Class is a keyword that indicates that classes
    • Create the object, and then add parentheses to the class name

Python Object-oriented introduction

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.