Is Python object-oriented or process-oriented?

青灯夜游
Release: 2023-01-05 16:54:49
Original
10437 people have browsed it

Python is object-oriented. From the beginning of its design, the Python language was positioned as an object-oriented programming language. "Everything in Python is an object" is the perfect interpretation of the Python programming language. Classes and objects are important features of Python. Compared with other object-oriented languages, Python can easily create a class and object. At the same time, Python also supports the three major features of object-oriented: encapsulation, inheritance and polymorphism.

Is Python object-oriented or process-oriented?

The operating environment of this tutorial: windows7 system, python3 version, DELL G3 computer

Although Python is an interpreted language, the Python language is designed From the beginning, it was positioned as an object-oriented programming language. "Everything in Python is an object" is a perfect interpretation of the Python programming language.

What is object-oriented

Object-oriented programming is developed on the basis of process-oriented programming. It has more features than process-oriented programming. Strong flexibility and scalability. Object-oriented programming is a watershed in the development of programmers. Many beginners will give up learning programming because they cannot understand object-oriented programming.

Object-oriented Programming (OOP for short) is a method of encapsulating code. In fact, in the study of previous chapters, we have already come into contact with encapsulation. For example, throwing messy data into a list is a simple encapsulation, which is the data level encapsulation; packaging commonly used code blocks into a function , which is also a kind of encapsulation, at the statement level.

Code encapsulation actually hides the specific code that implements the function, leaving only the interface for the user. Just like using a computer, the user only needs to use the keyboard and mouse to implement some functions. There is no need to know how it works internally.

Object-oriented programming is also an encapsulation idea, but it is obviously more advanced than the above two encapsulations. It can better simulate things in the real world (treat them as objects), and Encapsulate the data and code blocks (functions) that describe the characteristics together.

For example, if you design a turtle character in a game, how should you implement it? It will be simpler to use object-oriented thinking, which can be described in the following two aspects:

  • Describe from the surface characteristics, for example, green, has 4 legs, weighs 10 kg , with shell and so on.

  • Describe it based on its behaviors, for example, it can crawl, eat, sleep, retract its head and limbs into its shell, etc.

If the turtle is represented by code, its surface characteristics can be represented by variables, and its behavioral characteristics can be represented by establishing various functions. The reference code is as follows:

class tortoise:
    bodyColor = "绿色"
    footNum = 4
    weight = 10
    hasShell = True
    #会爬
    def crawl(self):
        print("乌龟会爬")
    #会吃东西
    def eat(self):
        print("乌龟吃东西")
    #会睡觉
    def sleep(self):
        print("乌龟在睡觉")
    #会缩到壳里
    def protect(self):
        print("乌龟缩进了壳里")
Copy after login

Note that the above code is only to demonstrate object-oriented programming ideas.

Therefore, from a certain program perspective, using object-oriented thinking can better simulate things in real life than using only variables or only functions.

Not only that, in Python, all variables are actually objects, including integer (int), floating point type (float), string (str), list (list), tuple (tuple) ), dict and set. Take dictionary (dict) as an example. It contains multiple functions for us to use. For example, use keys() to get all the keys in the dictionary, use values() to get all the values ​​in the dictionary, and use item() to get all the keys in the dictionary. Value pairs, etc.

Object-oriented related terms

Before systematically learning object-oriented programming, beginners should understand some terms about object-oriented. Knowing the correct terminology can be helpful when discussing code with others, or when trying to find solutions to problems we encounter.

In object-oriented, common terms include:

  • Class: It can be understood as a template through which countless specific instances can be created. For example, the tortoise written earlier represents only the species of turtle, through which countless instances can be created to represent turtles with various characteristics (this process is also called instantiation of a class).

  • Object: Classes cannot be used directly, only instances (also called objects) created through the class can be used. This is a bit like the relationship between car drawings and cars. The drawing itself (class) cannot be used by people, only a car (object) created through the drawing can be used.

  • Attributes: All variables in a class are called attributes. For example, in the tortoise class, bodyColor, footNum, weight, and hasShell are all properties owned by this class.

  • Methods: All functions in a class are usually called methods. However, unlike functions, class methods must contain at least one self parameter (more on this later). For example, in the tortoise class, crawl(), eat(), sleep(), and protect() are all methods owned by this class. Class methods cannot be used alone and can only be used together with objects of the class.

Object-oriented features of Python:

Classes and objects are important features of Python. Compared with other object-oriented Language, Python makes it easy to create a class and object. At the same time, Python also supports the three major characteristics of object-oriented: encapsulation, inheritance and polymorphism.

Encapsulation

The term object (Object) in object-oriented programming can basically be regarded as data (properties) and a series of data that can access and operate these data. A collection of methods. The traditional "program =
data structure algorithm" is encapsulated, "covered" and simplified to "program = object message". Objects are instances of classes, and the abstraction of classes needs to be encapsulated. Encapsulation allows the caller to use the object directly without caring about how the object is constructed.

Inheritance

  • Class inheritance:

    The direct feeling of inheritance is that it is a behavior of reusing code . Inheritance can be understood as establishing a special class object based on an ordinary class. The subclass has an IS-A relationship with the parent class it inherits.

  • Multiple inheritance:

    Unlike C#, Python supports multiple class inheritance (C# can inherit from multiple Interfaces, but at most one class). The multiple inheritance mechanism is sometimes useful, but it can easily complicate things.

Polymorphism

Polymorphism means that the same operations can be used on different objects, but they may appear in multiple forms result. In Python, polymorphism is used whenever you don't know what type an object is, but you need the object to do something. Methods are polymorphic and so are operators.

【Related recommendations: Python3 video tutorial

The above is the detailed content of Is Python object-oriented or process-oriented?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template