Writing a high-quality code: 91 Suggestions for improving Python programs

Source: Internet
Author: User

Write high-quality code: 91 Suggestions for improving Python programs http://book.douban.com/subject/25910544/

1. (recommended) is used to determine whether the ID of two objects is equal, = = is the judgment value is equal

2. (recommended) For,while, and the try statement can be followed by the Else statement, you can simplify the code

3. (recommended) __init__.py module can write code, import only need import package (folder)

4. (recommendation 19) use Import a instead of from a import B to avoid errors caused by nested imports

Try not to use import *
Add return to the 5.finally statement, or break will mask the exception that jumps out, so don't use return and break in finally


7. (recommendation 32) default values for default parameters do not use do not set as mutable objects, such as lists, dictionaries, etc.
8. (recommendation 30) List parsing can be multiple loops,

list1=list2=[1,2,3]
print [(A, B) for a in List1-B in List2 if a==1]


9. (recommendation 30) (1) represents the string (1,) is the ancestor
10. (recommendation 36) string manipulation

Partition method,

Src= ' ABCDEFG '
Print src.partition (' de ') # (' abc ', ' de ', ' FG ')

Split () can have no parameters,

11. (Recommended) Defaultdict

From collections Import Defaultdict

Dict1=defaultdict (list)

dict1[' A '].append (' B ')

No error.

12. (recommendation 39) Counter module for quantity statistics

From collections Import Counter

Writing a high-quality code: 91 Suggestions for improving Python programs

Related Article

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.