Python design pattern object oriented programming

itread01 2021-01-23 16:43:33
python design pattern object oriented


This article is based on the geek time king contest 《 Beauty of design pattern 》 Make a summary and own understanding .
When it comes to object-oriented programming , As a qualified Pythoner, It can be said that it's easy to come by . After all Python Li “ All things are objects ” Well , If someone says you can't program object-oriented at all , Or don't understand object orientation at all , That might get your abuse , At the end of the day , Do you really know object oriented ? Try to answer the following questions :

  • Object oriented programming is defined as ?Python Why object oriented programming language ?Python All things in it are objects. What is the specific form of expression ?
  • What are the four features of object orientation ? stay Python Do you have all these four points in the book ? How to realize it ?

Tips : If the answer to the above question is clear 、 Very clear , That article can be skipped , If you don't feel clear , That article will help you code more or less !

Introduction to design patterns

Before we talk about object-oriented programming , Let's look at design patterns first , Design patterns are mainly formalized by programming 、 Design principles 、 Design patterns constitute , See the figure below for details :

There are a lot of concepts in Turin , But the initial grasp of , Just have a general impression .

Talk about object orientation

Object oriented history

What is object-oriented , From history , We know Python The official release is 1991 year ,Linux Official release is also 1991 year ,Java The official release is 1995 year ,Go The official release is 2009 year , The historical development of object-oriented concept is :1960 Year of simula The concept of class and object is proposed for the first time , And then 70 The programming language of the '80s Smalltalk It's the first time to use the object-oriented concept , It can be seen that the concept of object orientation was put forward early , And with the development of software , Most of the languages that followed became object-oriented languages , Is there a process oriented language ? yes , we have ,C Language is a typical process oriented language .
If you are interested, you can read Wikipedia , Learn about the history of these languages .

  • Object oriented - Wikipedia
  • Python- Wikipedia
  • Java- Wikipedia
  • Linux- Wikipedia
  • Go- Wikipedia
Object oriented concepts

Object oriented , namely object-oriented, It is subdivided into object-oriented analysis 、 Object oriented design 、 Object oriented programming , namely OOA(Analysis)、OOD(Design)、OOP(programming).
If you read Wikipedia , Maybe it's still fuzzy , Let's know the key words through the figure below : Normalization of programming 、 Contains properties and methods 、 Is an example of a class 、 The basic unit of a program 、 The purpose is to improve the expansibility and flexibility of the software . That sentence is : Object oriented is a formalization of programming , Take class and object as basic unit , By encapsulation 、 abstract 、 Polymorph 、 Inherit these four characteristics ( It's not a strong demand ) To achieve code design , The purpose is to improve the maintainability of software 、 Expandable Suite 、 Reusability . Of course, the above is my personal words , Not professional , But the meaning is right .

that Python Why is object-oriented programming language , Obviously , It provides classes and objects to organize code , It also has four characteristics , Naturally, it is . But if you don't have four features, you must be an object-oriented programming language ? Obviously there is no reference definition , With the development of software , Many languages are divorced from the four characteristics , such as Java Although it supports inheritance , But it doesn't have much inheritance ; such as Go Just give up inheriting such features , and Java and Go There's more interface This interface property , stay Python Li doesn't support , But all three are object-oriented programming languages .

Everything is an object

We often talk about Python Everything is an object , How to understand this sentence ? It's simple , stay Python When we want to construct a class object , It's all about inheritance object On the premise of , So judge “ All things ” Is it an object , Just judge whether it belongs to the object , The concept of everything here is very broad , The more common ones are : Numbers 、 String 、 Brin 、 For example, function , The results are as follows :

Why are all object types ? Let's take the brin value as an example , Through dir There are a lot of properties and methods in it ( Here's the picture ), Then these methods can't come out of thin air , So it's all inherited , Then it's clearer , When we use variables to refer to numbers 、 And so on , In fact, one object after another is constructed , These objects have magical methods that enable them to support a range of operations , such as __lt__ So that it has a relatively small capacity , such as __eq__ So that it has the ability to wait for judgment .

Object oriented features

About the four characteristics : Package 、 abstract 、 Inherit 、 Polymorph , You can see the summary in the figure below :

Python There are four characteristics of natural support , I used to be vague about encapsulation and abstraction , Both encapsulate abstract common code , And then provide it to other methods to call , This understanding is one-sided , Because the meaning of the two is very different . Encapsulation is used to hide implementation and protect data , such as Python We often define private types in classes for external programs to call ( Here's the picture ), Here you can see a It is equivalent to Java Of public Keywords , Is to allow any call ; and _a It is Pythoner Rio's established private method , If you call pycharm This ide It will use wavy lines to prompt users that it is an illegal reference ; If it is __a, It is equivalent to Java Of private, If an external call ide It will be marked with yellow to indicate an error .

The results are as follows :

As for abstraction , The broad sense is to extract common code , The corresponding methods for external exposure ; The narrow sense of understanding is the concept of interface , Only the methods to be exposed are included in the interface class , Without revealing the specific implementation , That is to say “ Based on the interface rather than implementing programming ”. It is a pity that , stay Python The interface feature is not available in , If you're fuzzy about the interface , You can search for lower interface classes interface, I believe I will learn a lot of information . but Python You can go through duck-typing And abstract base classes to implement extra abstraction , In this follow-up article, I will introduce in detail .
In addition, inheritance and polymorphism are the basic syntax , I won't elaborate here .


That's all for this article , Although it's all about the foundation , But I believe that if you read it, there will still be some harvest


  1. Experience of learning Python
  2. python7、8章
  3. Chapter 7 and 8 of Python
  4. python bool和str转换
  5. python——循环(for循环、while循环)及练习
  6. python变量和常量命名、注释规范
  7. python自定义异常捕获异常处理异常
  8. python 类型转换与数值操作
  9. python 元组(tuple)和列表(list)区别
  10. 解决python tkinter 与 sleep 延迟问题
  11. python字符串截取操作
  12. Python bool and STR conversion
  13. Python -- loop (for loop, while loop) and Practice
  14. Specification for naming and annotating variables and constants in Python
  15. Python custom exception capture exception handling exception
  16. Python type conversion and numerical operation
  17. The difference between tuple and list in Python
  18. Solve the delay problem of Python Tkinter and sleep
  19. Python string interception operation
  20. Python 100天速成中文教程,GitHub标星7700
  21. Python 100 day quick Chinese course, GitHub standard star 7700
  22. 以我的親身經歷,聊聊學python的流程,同時推薦學python的書
  23. With my own experience, I'd like to talk about the process of learning Python and recommend books for learning python
  24. python爬虫获取起点中文网人气排行Top100(快速入门,新手必备!)
  25. Python crawler to get the starting point of Chinese network popularity ranking Top100 (quick start, novice necessary!)
  26. 【Python常用包】itertools
  27. Itertools
  28. (国内首发)最新python初学者上手练习
  29. (国内首发)最新python初学者上手练习
  30. (first in China) the latest practice for beginners of Python
  31. (first in China) the latest practice for beginners of Python
  32. (数据科学学习手札104)Python+Dash快速web应用开发——回调交互篇(上)
  33. (data science learning notes 104) Python + dash rapid web application development -- callback interaction (Part 1)
  34. (数据科学学习手札104)Python+Dash快速web应用开发——回调交互篇(上)
  35. (data science learning notes 104) Python + dash rapid web application development -- callback interaction (Part 1)
  36. (資料科學學習手札104)Python+Dash快速web應用開發——回撥互動篇(上)
  37. (materials science learning notes 104) Python + dash rapid web application development -- callback interaction (Part 1)
  38. Python OpenCV 图片高斯模糊
  39. Python OpenCV image Gaussian blur
  40. Stargan V2: converse image synthesis for multiple domains reading notes and Python code analysis
  41. 零基础入门Python:基本命令、函数、数据结构
  42. Python: basic commands, functions and data structures
  43. 毫无基础的人如何入门Python?从入门到进阶三份教程,拿走不谢
  44. How can a person without foundation get into Python? From the introduction to the advanced three tutorials, take away
  45. Python设计模式面向对象编程
  46. Python design pattern object oriented programming
  47. Python设计模式面向对象编程
  48. Python design pattern object oriented programming
  49. 怎么样描述你的数据——用python做描述性分析
  50. GitHub上3k+star的python爬虫库你了解吗?详解MechanicalSoup爬虫库
  51. python数据分析——在python中实现线性回归
  52. 疫情来袭,30分钟学会用python开发部署疫情可视化网站
  53. How to describe your data
  54. Do you know the python crawler Library of 3K + star on GitHub? Mechanical soup crawler Library
  55. Python data analysis -- realizing linear regression in Python
  56. When the epidemic strikes, learn to develop and deploy the visualization website of epidemic situation with Python in 30 minutes
  57. 手机上利用python进行数据分析——创建自己的远程jupyter notebook
  58. python数据类型的强制转换
  59. Using Python for data analysis on mobile phones -- creating your own remote jupyter notebook
  60. Mandatory conversion of Python data type