91 suggestions for writing high-quality code and improving Python programs

Posted May 25, 20205 min read

The content of this article is composed of the best practices of Python coding. In-depth discussion of the skills of writing high-quality Python code from 8 aspects of basic principles, idioms, syntax, libraries, design patterns, internal mechanisms, development tools and performance optimization A total of 91 valuable suggestions were summed up with taboos. Each suggestion corresponds to a problem that Python programmers may encounter.

Recommendation 1:Understand Pythonic concepts

Recommendation 2:Write Pythonic code

Recommendation 3:Understand the differences between Python and C

Recommendation 4:Appropriate comments in the code

Recommendation 5:By properly adding empty exercise code layout is more elegant and reasonable

Recommendation 6:4 principles for writing functions

Recommendation 7:Centralize constants into one file

Recommendation 8:Use assert statements to find problems

Recommendation 9:Intermediate variables are not recommended for data exchange

Recommendation 10:Make full use of the characteristics of Lazy evaluation

Recommendation 11:Understand the shortcomings of enumeration alternative implementation

Recommendation 12:Type is not recommended for type checking

Recommendation 13:Try to convert to floating-point type before doing division

Recommendation 14:Beware of eval() security holes

Recommendation 15:Use enumerate() to get the index and value of the sequence iteration

Recommendation 16:Distinguish = Applicable scenarios with is

Recommendation 17:Consider compatibility and use Unicode whenever possible

Recommendation 18:Build a reasonable package hierarchy to manage modules

Recommendation 19:Use from… import statements sparingly

Recommendation 20:Give priority to using absolute import to import modules

image

Recommendation 21:i + = 1 is not equal to ++ i

Recommendation 22:Use with to automatically close resources

Recommendation 23:Use the else clause to simplify the loop(exception handling)

Recommendation 24:Follow some basic principles of exception handling

Recommendation 25:Avoid possible pitfalls in finally

Recommendation 26:Deep understanding of None, correctly determine whether the object is empty

Recommendation 27:Join strings should prefer join over +

Recommendation 28:Try to use .format instead of%when formatting strings

Recommendation 29:Treat mutable objects and immutable objects differently

Recommendation 30:\ [],() and {}:consistent container initialization form

image

Recommendation 31:Remember that function parameters are neither passed by value nor by reference

Recommendation 32:Beware of potential problems with default parameters

Recommendation 33:Use variable length parameters with caution

Recommendation 34:Understand the difference between str() and repr()

Recommendation 35:Distinguish the applicable scenarios of staticmethod and classmethod

Recommendation 36:Master the basic usage of strings

Recommendation 37:Choose sort() or sorted() as needed

Recommendation 38:Use the copy module to copy objects deep

Recommendation 39:Use Counter for counting statistics

Recommendation 40:In-depth knowledge of ConfigParser

image

Recommendation 41:Use argparse to process command line parameters

Recommendation 42:Use pandas to process large CSV files

Recommendation 43:Use ElementTree to parse XML in general

Recommendation 44:Understand the pros and cons of pickle modules

Recommendation 45:Another good option for serialization—JSON

Recommendation 46:Use traceback to obtain stack information

Recommendation 47:Use logging to record log information

Recommendation 48:Use the threading module to write multi-threaded programs

Recommendation 49:Use Queue to make multithreaded programming safer

Recommendation 50:Use the module to implement the singleton model

The public account Feng Feng Qi Pen focuses on sharing Java, Python, personal growth, professional development dry goods, pay attention to the excellent programming video tutorials organized by editors; every day at 14:40 pm wonderful push, programmers are worthy of attention Technology platform.

Recommendation 51:Use mixin mode to make the program more flexible

Recommendation 52:Use the publish-subscribe model to achieve loose coupling

Recommendation 53:Beautify code with state mode

Recommendation 54:Understand build-in objects

Recommendation 55:init() is not a constructor

Recommendation 56:Understand the name lookup mechanism

Recommendation 57:Why the self parameter is required

Recommendation 58:Understand MRO and multiple inheritance

Recommendation 59:Understand the descriptor mechanism

Recommendation 60:Distinguish between getattr() and getattribute() methods

image

Recommendation 61:Use safer properties

Recommendation 62:Master metaclass

Recommendation 63:Be familiar with the Python object protocol

Recommendation 64:Use operator overloading to implement infix syntax

Recommendation 65:Familiar with Python's iterator protocol

Recommendation 66:Be familiar with Python generators

Recommendation 67:Builder-based coroutines and greenlets

Recommendation 68:Understand the limitations of GIL

Recommendation 69:Object management and garbage collection

Recommendation 70:Install the package from PyPI

image

Recommendation 71:Use pip and yolk to install and manage packages

Recommendation 72:Create paster package

Recommendation 73:Understand unit testing concepts

Recommendation 74:Write unit tests for packages

Recommendation 75:Use test-driven development to improve code testability

Recommendation 76:Use Pylint to check code style

Recommendation 77:Conduct an efficient code review

Recommendation 78:Publish the package to PyPI

Recommendation 79:Understand the basic principles of code optimization

Recommendation 80:With the help of performance optimization tools

image

Recommendation 81:Use cProfile to locate performance bottlenecks

Recommendation 82:Use memory \ _profiler and objgrash to analyze memory usage

Recommendation 83:Try to reduce algorithm complexity

Recommendation 84:Master the basic skills of loop optimization

Recommendation 85:Use generators to increase efficiency

Recommendation 86:Use different data structures to optimize performance

Recommendation 87:Take full advantage of set

Recommendation 88:Use meltiprocessing to overcome the defects of GIL

Recommendation 89:Use thread pools to improve efficiency

Recommendation 90:Use C/C ++ module extensions to improve performance

Recommendation 91:Use Cython to write extension modules

The public account Feng Feng Qi Pen focuses on sharing Java, Python, personal growth, professional development dry goods, pay attention to the excellent programming video tutorials organized by the editor; every day at 14:40 pm, wonderful push, every programmer is worth Technology platform of concern.