Some habits of python programming

小云云
Release: 2018-03-30 16:39:39
Original
1848 people have browsed it

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template