- Using local variables
Use local variables instead of global variables
2. Reduce function calls
(1) Use isinstance() as much as possible, id() as much as possible, and type() as much as possible.
Len (a) is called every time through the loop
Len (a) will only be executed once
3. Use dictionaries instead of multiple conditional judgments
4. Traversing an object is faster than traversing its index
5. Replace list generators with generators
Because list generation will generate all the elements at once, if there are too many elements, it will take up a lot of memory. Therefore, if you want to learn Python, it is necessary to listen to the teacher’s class and receive python benefits. If you want to learn Python, you can go to Wei Xin of Teacher Mengya: The front group is: Mengy, the back group is: 7762, put the above two groups of letters together in order, she will arrange to learn.
Generators only execute when called
6. Compile and call
When executing code using the eval() and exec() functions, it is better to use compile() first rather than directly to avoid double compilation problems
The same is true with re.compile().
7. Module writing
This code without indentation is executed when importing a module. Therefore, you should put your code in functions.
The test code can be written in the module’s main(), and while the code is running, the value of name is checked. If it is main(indicating that the module is run directly, the main() function is called), and if it is the module’s name (indicating that the module is called), the test is not executed.