Python programs written in high-quality 91 recommendations (rpm)

Recommendation 1: Understanding the concept Pythonic

Pythonic can be defined as: fully reflect its own characteristics Python coding style.
Recommended reading: PEP 8 - Style Guide for Python Code

Recommendation 2: Writing Pythonic Code

  1. Avoid only case to distinguish different objects.
  2. Avoid using easily cause of confusion as the letter 'O' of the case and the letter 'L' lowercase 'l'.
  3. Do not be afraid of long variable names, variable names easier to understand the most important.

Recommendation 3: understand the difference Python and C language at

  1. "Indentation" and "{}"
    to separate the codes C, C ++, Java and other languages using braces {}, Python is used strictly indentation (4 spaces or tab. 1) Note and tab spaces can not be mixed.
  2. Single quote ( ') and double quotes ( ")
    C representing the character in single quotes, double quotes indicate a string, while in Python, no significant difference between single and double quotation marks.
  3. Ternary operator (? :)
    ternary operator is a shorthand method if ... else, grammatical form C? X: Y, represents C is True when taking X, Y. On the contrary take No ternary operator in Python, but equivalent form: X if C else Y.
  4. ... Case Switch
    Python is not conditional branching statements like C language like that, but if ... elif statement can be used to achieve the same functionality.
    Recommendation 4: appropriate to add comments in the code

There are three forms of Python code comments: block comments, comment, and document comment line.
Use the comment recommend the following:

  1. Only comment those complex operations, algorithms, there is not enough clear code.
  2. Notes and code separated by a certain distance, not too tight, the best comments left block after a few lines blank.
  3. Functions and methods accessible to external add documentation comments.

Recommendation 5: empty exercise by adding appropriate code layout more elegant, reasonable

The figure below for reference
Here Insert Picture Description
Recommendation 6: Write four principle functions

  1. Function is designed to be short as much as possible, the nesting level should not be too deep.
  2. Function declaration should be reasonable, simple, easy to use (the function name is easy to understand, concise parameters).
  3. Function design parameters should be considered backward compatibility (easy to develop subsequent versions).
  4. A function of only one thing. Try to ensure consistency function statement granularity.
    Recommendation 7: The constant focus to a file

This is rarely used, it can be used to achieve a constant feature custom class.
Recommendation 8: use assert statements to identify problems

Assertion (assert) in many languages exist, mainly for the debugger service to quickly and easily input abnormal or inappropriate inspection procedures and so on.
Note that use assert comes at a price, it will have some impact on performance, you can not try to do.
Recommendation 9: Data exchange value of time is not recommended to use intermediate variables

In Python, the value of two variables need not be exchanged intermediate variables

a = 10
b = 11

#不推荐的做法
tmp = a        
a = b
b = tmp       

#pythonic的交换方式,无需中间变量且效率更高
a, b = b, a    
12345678910

Recommendation 10: Take advantage of the characteristics of Lazy evaluation

Lazy evaluation can be understood as an inert delay calculation or calculations, can bring benefits are twofold:

  1. Avoiding unnecessary computation to improve efficiency
    of the conditional expression in Python if x and y, if x is a case where the direct return False False, the program does not calculate the value of y expression. For if x or y, when x is True, the calculated value of y does not direct expression and returns True.
  2. Save space, the data structure called an infinite loop may
    builder to find out? Python iterators and generators
  • Recommendation 11: the enumeration alternative real *
  • Recommendation 12: It is recommended to use type *
  • Recommendation 13: try to convert the floating-point type *
  • Recommendation 14: vigilance eval () is *
  • Recommendation 15: Use enumerate () Gets a sequence of iterations *
  • Recommendation 16: distinguish and is the * =
  • Recommendation 17: Consider compatibility, as far as possible the use Uni *
  • Recommendation 18: Building a reasonable package hierarchy to manage mo *
  • Recommendation 19: Use sparingly from ... impo *
  • Recommendation 20: Priority to use absolute import *
  • I recommend 21: i + = 1 * is not equal to
  • Recommendation 22: Use with automatic *
  • Recommendation 23: Use the simplified loop else clause (iso *
  • Recommendation 24: follow the following exception handling *
  • Recommendation 25: Avoid finally may send *
  • Recommendation 26: in-depth understanding of None, the right to judge the object *
  • Recommendation 27: The connection string should be preferred join *
  • Recommendation 28: .format way to make use of when format string *
  • Recommendation 29: The variable objects and do not discriminate *
  • Recommendation 30: First container consistent *: [], (), and {}
  • Recommendation 31: Remember the function parameter passing by value is neither nor *
  • Recommendation 32: alert to potential default parameters *
  • Recommendation 33: caution *
  • Recommendation 34: In-depth understanding str () and repr (*
  • Recommendation 35: distinguish staticmethod and classmethod of *
  • Recommendation 36: grasp the string *
  • Recommendation 37: on-demand selection sort () or sort *
  • Recommendation 38: Use the copy module D *
  • Recommendation 39: Using Counter conduct *
  • Recommendation 40: In-depth grasp ConfigPa *
  • Recommendation 41: Use argparse processing command *
  • Recommendation 42: Use the pandas with large C *
  • Recommendation 43: general use ElementTree solution *
  • Recommendation 44: understanding module pick *
  • Recommendation 45: serialized another good choice - *
  • Recommendation 46: To use the traceback won *
  • Recommendation 47: Use the logging record *
  • Recommendation 48: Use multi-threading module to write *
  • Recommendation 49: Use the Queue multithreading encoding *
  • Chapter 5 *
  • Recommendation 50: module for use *
  • 51 recommendations: use mixin mode allows program *
  • 52 suggestions: practical publish subscribe model *
  • Recommendation 53: The state mode *
  • Chapter 6 *
  • Recommendation 54: Understand build-in obj *
  • Recommendation 55: init () not *
  • Recommendation 56: Understanding Name *
  • Recommendation 57: Why do we need se *
  • Recommendation 58: Understanding MRO *
  • Recommendation 59: Understand description *
  • Recommendation 60: The difference between getattr () and getattribute *
  • Recommendation 61: Use safer prop *
  • Recommendation 62: Master metac *
  • 63 suggestions: familiar with Python *
  • Recommendation 64: Operator overloading achieve *
  • Recommendation 65: The familiar Python Diego *
  • 66 suggestions: familiar with Python *
  • 67 recommendations: Based generator coroutines and gree *
  • Recommendation 68: Understanding GIL *
  • Recommendation 69: Managing object and *
  • Chapter 7 Using auxiliary tools *
  • Recommendation 70: from PyP *
  • Recommendation 71: Installation using pip and yolk *
  • 72 recommendations: do paste *
  • 73 recommendations: Understand units *
  • Recommendation 74: In order to write the package *
  • 75 recommendations: use test-driven development to improve the code *
  • 76 suggestions: Use Pylint check *
  • 77 recommendations: efficient *
  • Recommendation 78: The package published to *
  • Chapter 8 Performance section *
  • 79 suggestions: Learn code optimization *
  • Recommendation 80: With Performance *
  • 81 recommendations: use cProfile positioning *
  • 82 suggestions: Use memory_profiler and objgrash analysis *
  • Recommendation 83: efforts to reduce operator *
  • Recommendation 84: master cycle optimization *
  • Recommendation 85: Using generators *
  • 86 recommendations: use different data structures *
  • 87 suggestions: take advantage of se *
  • 88 suggestions: Use meltiprocessing overcome GI *
  • 89 suggestions: Use thread pool *
  • 90 recommendations: use C / C ++ module expansion *
  • 91 suggestions: Use Cython write extension modules

Guess you like

Origin www.cnblogs.com/zhuyuanying123--/p/11069768.html