Write an authentic Python code like a pythonista: idiomatic Python

Source: Internet
Author: User

Original address http://python.net /~ Goodger/projects/pycon/2007/idiomatic/handout.html. I have chosen some important translations. In short,CodeIt is written to people, not to machines.


    • White (1)

(1: four spaces are added for each indent level;

(2: Do not use the tab key (I have been using the tab key for a long time, which is very convenient. This is a bit unreasonable );

(3: Do not mix spaces and Tab keys;

(4: An empty line between functions;

(5: two empty rows between classes;


    • White (2)

(1) Leave a space after the "," Sign of dicts, lists, tuples, and function functions; leave a space after the ":" sign of dicts;

(2: Leave a space for each assignment and comparison;

(3: Do not leave the parameters that are placed next to the right or left brackets blank, and do not leave them blank before the function parameter list;

(4: Do not leave it white in docstrings


    • Name

(1: joined_lower, function, method, and attribute usage;

(2: joined_lower or all_caps, suitable for constant definition (Personal tends to all_caps );

(3: studlycaps, class name;

(4: camelcase, which is generally not used unless it is a continuation of the old Code habits;

(5: attribute interface, _ internal, _ private. Do not use _ private as much as possible. The reason is not translated. If it is long, it will be OK.


    • Long sentences and connections

(1: Keep a row within 80 characters;

(2) (), [], {} contains the hidden cross-row connection function;

  • Def _ init _ (self, First, Second, Third,

  • Fourth, Th, sixth ):

  • Output = (first + second + third

  • + Fourth + Th + sixth)

(3: In other cases, "\" is used for connection. Note that "\" must be the last letter of this line. The function of connecting any character will be invalid.

    • Verylong. left_hand_side \

    • = Even_longer.right_hand_side ()


    • Long String

(1: the adjacent string is merged into a string by the parser;

(2: strings with the R prefix are not escaped;

(3: "+" is required for string variable connection ";

(4) use "Or ''' for cross-row strings ''''''.


    • Document strings and comments

Document string (docstring) is used to describe the purpose and usage of functions (for Users)

(1: Explains functions of a function;

(2: describes parameters, returned values, and possible exceptions;

(3: it is best to describe the call method, especially when this function has a very coupled caller.

Annotations are used to describe the implementation method of functions and maintain code (for developers and maintenance code owners)

(1 :#!!! BUG :...

(2 :#!!! Fix: This is a hack

(3 :#??? Why is this here?


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.