Some of the habits of Python programming

Source: Internet
Author: User
This article is mainly to share with you some Python programming habits, mainly in the way of code and we explain, hope to help everyone.

1. Write the program document (format, auto indent (vim setting line width and autoindent), note) as follows:

2, as far as possible to streamline the code:

Such as


Can be written as:

return element in Sub_list

3. The code should not be coupled with the data:

If try not to write OS.GETCWD () in the script to get the path of the current script, so that the path obtained is Python execution of the path of the bash, if the execution of the bash path changes will cause the program is not reproducible. Workaround: Pass the path as a python execution parameter (python filename.py script_path) or through Os.path.split (Os.path.realpath ("__file__")) [0], Such paths are not limited by the bash execution path. Of course, in general, the first direct route is the best way.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.