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 :https://yyk.fh21.com.cn/hexpert_6369.html Xinyang see infertility hospital :https://yyk.fh21.com.cn/hospital_6369/tsyl.html Zhumadian infertility hospital :https://yyk.fh21.com.cn/hospital_6369/hj.html