This article mainly shares with you some habits of python programming. It mainly explains it to you in the form of code. I hope it can help you.
1. Write the program documentation (format, automatic indentation (vim sets line width and autoindent), comments) in the following form:
2. Try your best Simplified code:
Such as
can be written as:
return element in sub_list
3. The code should not be coupled with the data:
For example, try not to write os.getcwd() in the script to obtain the path of the current script. The path obtained in this way is the path of bash where python is executed. If the bash path of execution changes, the program will not be reproducible. The solution is: pass the path as a python execution parameter (python filename.py script_path) or pass os.path.split(os.path.realpath("__file__"))[0], so that the path acquisition will not be affected by bash Limitations on execution paths, etc. Of course, in general, the first direct transmission path method is the best.
Related recommendations:
Quick Start Examples of Python Programming
The above is the detailed content of Some habits of python programming. For more information, please follow other related articles on the PHP Chinese website!