Python programming skills to improve work efficiency

SXXpenguin 2020-11-12 16:11:09
python programming skills improve work

Python Programming skills improve work efficiency , In the process of learning and working, mastering some small skills can greatly improve the efficiency of work , Next, I will introduce programming idioms 、 Basic usage 、 Library usage 、 Internal mechanism 、 Using tools to assist project development 、 Performance analysis and optimization programming skills .

One 、Python Program introduction

1、 understand Pythonic Concept , See Python Medium 《Python zen 》

2、 To write Pythonic Code

(1) Avoid irregular code , For example, only case sensitive variables 、 Use confusing variable names 、 Fear of too long variable names, etc . Sometimes a long variable name makes the code more readable .

(2) Learn more Python Related knowledge , For example, language features 、 Library characteristics, etc , such as Python Evolution process, etc . Learn one or two industry recognized Pythonic Code base for , such as Flask etc. .

3: understand Python And C The difference , Like indenting and {}, Single quotation mark and double quotation mark , ternary operator ?, Switch-Case Statement etc. .

4: Add comments to your code as appropriate

5: Adding empty exercise code layout is more reasonable

6: Write the 4 Principles

(1) Function design should be as short as possible , Nesting level should not be too deep

(2) Function declaration should be reasonable 、 Simple 、 Easy to use

(3) Function parameter design should consider downward compatibility

(4) A function does only one thing , Try to ensure the consistency of function granularity

7: Centralize constants in one file , And try to use all capital letters for constant names

Two 、 Programming idioms

8: utilize assert Statements to find problems , But should pay attention to , Assertion assert Will affect efficiency

9: Temporary variables are not recommended for data exchange values , Instead of directly a, b = b, a

10: Make full use of inert computing (Lazy evaluation) Characteristics of , So as to avoid unnecessary calculation

11: Understand the pitfalls of enumerating alternative implementations ( The latest version Python Enumeration feature has been added to )

12: It is not recommended to use type To do type checking , Because sometimes type The results are not necessarily reliable . If there is a need , Use isinstance Instead of a function

13: Try to convert the variable to floating-point type before division (Python3 Don't worry about it later )

14: alert eval() Security vulnerability of function , It's kind of like SQL Inject

15: Use enumerate() Get index and value of sequence iteration at the same time

16: Distinguish between == and is The applicable scenarios of , Especially when comparing immutable variables such as strings ( See comments for details )

17: Use as much as possible Unicode. stay Python2 Middle coding is a headache , but Python3 You don't have to think about it

18: Build a reasonable package hierarchy to manage Module

3、 ... and 、 Basic usage

19: Moderate use from…import sentence , Prevent namespace pollution

20: priority of use absolute import To import modules (Python3 Has been removed from relative import)

21:i+=1 It's not equal to ++i, stay Python in ,++i The plus sign in front only indicates positive , No operation

22: Habitual use with Close resources automatically , Especially in document reading and writing

23: Use else Clause simplify loop ( exception handling )

24: Following the basic principles of exception handling

(1) Pay attention to the abnormal granularity ,try Write as little code as possible in the block

(2) Use individual carefully except sentence , or except Exception sentence , But to specific exceptions

(3) Pay attention to the order of exception capture , Handle exceptions at the right level

(4) Use more friendly exception information , Follow the specification of abnormal parameters

25: avoid finally Potential pitfalls in

26: In depth understanding of None, Correctly judge whether the object is empty .

27: Connection strings should take precedence join function , instead of + operation

28: Format strings as much as possible format function , instead of % form

29: Differentiate between mutable and immutable objects , Especially as a function parameter

30:[], {} and (): Consistent container initialization . Using list parsing makes the code clearer , More efficient at the same time

31: Function parameters , Neither value nor reference , It's a reference to a passing object

32: Be alert to potential problems of default parameters , Especially when the default parameter is mutable

33: Careful use of variable length parameter in function args and kargs

(1) It's too flexible to use , So that the function signature is not clear enough , Poor readability

(2) If we use variable length parameters to simplify the function definition because of too many function parameters , In general, the function can be reconstructed

34: In depth understanding of str() and repr() The difference between

(1) Different goals between the two :str Mainly for customers , Its purpose is readability , The return form is a string form with high user friendliness and readability ; and repr It's for Python Interpreter or Python Developer , Its purpose is accuracy , Its return value represents Python Definition inside the interpreter

(2) Entering variables directly in the interpreter , Default call repr function , and print(var) Default call str function

(3)repr The return value of a function can be eval Function to restore an object

(4) Both call the built-in function of the object __str__ () and __repr__ ()

35: Distinguish static method staticmethod And class methods classmethod Usage scenarios of

Four 、 Library usage

36: Master the basic usage of string

37: Select on demand sort() and sorted() function

sort() Is the list sorted in place , So you can't sort immutable types like tuples .

sorted() Can sort any type of iteration , Without changing the original variable itself .

38: Use copy Module deep copy object , Distinguish shallow copy (shallow copy) And deep copy (deep copy)

39: Use Counter Count statistics ,Counter Is a subclass of the dictionary class , stay collections Module

40: In depth ConfigParse

41: Use argparse Module processing command line parameters

42: Use pandas Handling large CSV file

Python Provide a CSV File processing module , And provide reader、writer Such as function .

Pandas Available in blocks 、 Consolidation, etc , Suitable for large data volume , And it is more convenient for two-dimensional data operation .

43: Use ElementTree analysis XML

44: Understanding module pickle The advantages and disadvantages of

advantage : The interface is simple 、 Common to all platforms 、 Wide range of data types supported 、 Extensibility is strong

Inferiority : Atomicity of data operation is not guaranteed 、 There are security issues 、 Incompatibility between different languages

45: Another option for serialization JSON modular :load and dump operation

46: Use traceback Get stack information

47: Use logging Logging information

48: Use threading Module programming multithreaded program

49: Use Queue Module makes multithreaded programming safer

5、 ... and 、 Design patterns

50: Using module to realize single instance mode

51: use mixin Patterns make programs more flexible

52: Publish with - Subscription mode for loose coupling

53: Beautify code with state mode

6、 ... and 、 Internal mechanism

54: understand build-in object

55:__init__ () Not a construction method , understand __new__ () The difference with it

56: Understand the search mechanism of variables , Scope

Local scope

Global scope

Nested scope

Built-in scope

57: Why self Parameters

58: understand MRO( Method parsing order ) And multiple inheritance

59: Understanding the descriptor mechanism

60: difference __getattr__ () And __getattribute__ () Differences between methods

61: Use safer property

62: Master metaclass metaclass

63: be familiar with Python Object protocol

64: Using operator overloading to implement infix Syntax

65: be familiar with Python Iterator protocol

66: be familiar with Python The generator

67: Generator based coroutine sum greenlet, Understanding the process 、 Multithreading 、 Differences between multiprocesses

68: understand GIL The limitations of

69: Object management and garbage collection

7、 ... and 、 Using tools to assist project development

70: from PyPI Install third party package

71: Use pip and yolk install 、 Management package

72: do paster Create a package

73: Understand the concept of unit testing

74: Writing unit tests for packages

75: Using test driven development (TDD) Improve code testability

76: Use Pylint Check code style

Code style review

Code error checking

Duplicate and unreasonable code found , Easy refactoring

Highly configurable and customizable

Support a wide variety of IDE Integration with editor

Can be based on Python Code generation UML chart

Can and Jenkins And other continuous integration tools , Support automatic code review

77: Conduct efficient code review

78: Publish package to PyPI

8、 ... and 、 Performance analysis and optimization

79: Understand the basic principles of code optimization

80: With performance optimization tools

81: utilize cProfile Locate performance bottlenecks

82: Use memory_profiler and objgraph Analyze memory usage

83: Try to reduce the complexity of algorithm

84: Master the basic skills of cycle optimization

Reduce the calculation inside the cycle

Change explicit loop to implicit loop , Of course, this will sacrifice the readability of the code

Try to reference local variables in the loop

Focus on inner nested loops

85: Using generators to improve efficiency

86: Use different data structures to optimize performance

87: make the best of set The advantages of

88: Use multiprocessing Module overcome GIL defects

89: Use thread pool to improve efficiency

90: Use Cythonb Write extension module Zhoukou infertility hospital : Xinyang see infertility hospital : Zhumadian infertility hospital :


  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database