Python design pattern object-oriented programming

Speaking of object-oriented programming, as a qualified Pythoner, it can be said to be easy. After all, "everything is an object" in Python. If others say that you don't know object-oriented programming at all, or don't understand object-oriented programming at all, then you may be vilified. After all, do you really understand object-oriented programming? Try to answer the following questions:

  • What is the definition of object-oriented programming? Why is Python an object-oriented programming language? What is the concrete representation of everything in Python as objects?
  • What are the four characteristics of object-oriented? Are these four points available in Python? How is it done?
  • Many people learn python and don't know where to start.
    Many people learn python and after mastering the basic grammar, they don't know where to find cases to get started.
    Many people who have done case studies do not know how to learn more advanced knowledge.
    For these three types of people, I will provide you with a good learning platform, free to receive video tutorials, e-books, and course source code! ??¤
    QQ group: 232030553

Tip: If the above questions are answered clearly and clearly, then this article can be skipped. If you feel unclear, then this article will give you more or less code help!

Introduction to design patterns

Before talking about object-oriented programming, let’s take a look at design patterns. Design patterns are mainly composed of programming paradigms, design principles, and design patterns, as shown in the following figure:

There are relatively many concepts in the map, but you only need to have a general impression if you have a preliminary grasp.

Talk about object-oriented

Object-oriented history

What is object-oriented? From history, we know that Python was officially released in 1991, Linux was officially released in 1991, Java was officially released in 1995, Go was officially released in 2009, and   the historical development of object-oriented concepts was: 1960 Simula first put forward the concept of class and object in 1970. Then the programming language Smalltalk in the 1970s first used the object-oriented concept. It can be seen that the object-oriented concept was put forward early, and with the development of software, most subsequent languages ​​have become object-oriented languages , Is there any process-oriented language? Yes, C language is a typical process-oriented language.

Those who are interested can read Wikipedia to understand the history of the development of these languages.

  • Object Oriented-Wikipedia
  • Python-Wikipedia
  • Java-Wikipedia
  • Linux-Wikipedia
  • Go-Wikipedia

Object-oriented concept

Object-oriented, namely object-oriented, is subdivided into object-oriented analysis, object-oriented design, and object-oriented programming, namely OOA (Analysis), OOD (Design), and OOP (programming).

If you read Wikipedia, it may still be very vague. You can know the keywords through the following figure: programming paradigm, containing attributes and methods, is an instance of a class, the basic unit of the program, and the purpose is to improve the scalability and flexibility of the software. That sentence is: Object-oriented is a programming paradigm, with classes and objects as the basic unit, through encapsulation, abstraction, polymorphism, inheritance these four characteristics (not strong requirements) to achieve code design, the purpose is to improve the software Maintainability, scalability, and reusability. Of course, the above is my personal sentence, not professional, but the meaning is in place.

So why Python is  an object-oriented programming language  . Obviously, it provides the characteristics of classes and objects to organize code, and it also has four major characteristics. That is naturally. But is it not necessarily an object-oriented programming language that does not have the four characteristics  ? Obviously this does not have a reference definition. With the development of software, many languages ​​have broken away from the four major features. For example, although Java supports inheritance, it does not have multiple inheritance; for example, Go directly abandons the feature of inheritance, while Java and Go have more The interface feature of interface is not supported in Python, but all three of them are object-oriented programming languages.

Everything is an object

We often talk about Python everything is an object, how do we understand this sentence? It's very simple. When we want to construct a class object in Python, we all assume that the inheritance of object is the prerequisite. So to judge whether "everything" is an object, just judge whether it belongs to an object. The concept of everything here is very broad. , The more common ones are: number, string, boolean, function as examples, the result is as follows:

Why are they all object types? Let's take the boolean value as an example. Through dir, we find that there are a large number of properties and methods (as shown in the figure below). These methods can't come out of thin air, so they are all inherited. Then it will be clearer. When we use variables to refer to numbers, strings, etc., we actually construct objects one after another. The magic methods of these objects enable them to support a series of operations, such as __lt__ which enables them to be smaller than , Such as __eq__ so that it has the ability to wait for judgment.

Object-oriented features

Regarding the four major characteristics: encapsulation, abstraction, inheritance, and polymorphism, you can see the summary below:

Python naturally supports four major features. In the past, it was always vague about encapsulation and abstraction. It is understood that both are encapsulated abstract common codes, and then provided to other method calls. This understanding is very one-sided, because the meaning of the two is very different. Big. Encapsulation is used to hide implementation and protect data. For example, in Python, we often define private types in classes for external programs to call (as shown in the figure below). Here you can see that a is equivalent to the public keyword of Java, which allows arbitrary calls; and _a is a private method established by convention in Pythoner. If an ide such as pycharm is called, the user will be prompted with a wavy line that it is an illegal reference; if it is __a, it is equivalent to Java's private. If the ide is called externally, it will be directly marked as yellow Indicates an error.

The results are as follows:

As for abstraction, the broad understanding is to extract the common code and expose the corresponding methods; the narrow understanding is the concept of interface. Only the methods to be exposed are included in the interface class without revealing the specific implementation, which is "based on Interface instead of programming". Unfortunately, the interface feature is not provided in Python. If you are vague about the interface, you can search for the interface class interface, I believe you will learn a lot of information. But in Python, you can additionally implement abstraction through duck-typing and abstract base classes, which will be introduced in detail in this follow-up article.

In addition, inheritance and polymorphism are the basic syntax, so I won't go into details here.

This is the end of this article. Although it is all basic, I believe that if you finish reading it, you will still have some gains.

Guess you like

Origin blog.csdn.net/Python_sn/article/details/113108543
Recommended