Python PEP8 encoding specification expressions and spaces in statements

Source: Internet
Author: User

Can't stand the Things

Avoid using extraneous spaces in the following situations:

    • Immediately after the parentheses, brackets, or curly braces.
Yes:spam (Ham[1], {eggs:2}) No:  spam (ham[1], {eggs:2})
    • Close to the comma, semicolon, or colon.
Yes:if x = = 4:print x, y; X, y = y, xno:  if x = = 4:print x, y; x, y = y, X
    • However, a colon is like a two-dollar operator in a slice and should have the same number of spaces on both sides (consider it the lowest-priority operator). In an extended slice operation, all colons must have the same spacing. Exception: When a slice parameter is omitted, the space is omitted.
      Recommended:

Ham[1:9], Ham[1:9:3], Ham[:9:3], Ham[1::3], Ham[1:9:]ham[lower:upper], Ham[lower:upper:], ham[lower::step]ham[lower+ offset:upper+offset]ham[: Upper_fn (x): Step_fn (x)], ham[:: Step_fn (x)]ham[lower + offset:upper + offset]

Not recommended

Ham[lower + Offset:upper + offset]ham[1:9], Ham[1:9], Ham[1:9: 3]ham[lower:: upper]ham[: Upper]
    • Close to the opening parenthesis of the function argument.
Yes:spam (1) No:  spam (1)
    • Close to the left parenthesis of the index or slice.
yes:dct[' key ' = lst[index]no:  DCT [' key '] = LST [index]
    • To align with another assignment statement, add more spaces to the assignment operator attachment.
      Recommended:
x = 1y = 2long_variable = 3

Not recommended:

X             = 1y             = 2long_variable = 3

Other recommendations
    • Avoid adding spaces at the tail. Because trailing spaces are usually invisible, there is confusion: for example, a newline character followed by a backslash is not counted as a continuation of the line. Some editors do not preserve trailing spaces, and many items (like CPython) filter out trailing spaces in Pre-commit hook calls.
    • Always add a space on both sides of the two-dollar operator: Assignment (=), increment assignment (+=,-=), Comparison (==,<,>,!=,<>,<=,>=,in,not,in,is,is not), Boolean (and, or, not).
    • If you use operators with different precedence, consider adding spaces around the operators with the lowest precedence. Sometimes you need to judge by yourself; however, do not use more than one space, and use the same number of spaces on both sides of the two-dollar operator.
      Recommended:
i = i + 1submitted + = 1x = X*2-1hypot2 = x*x + Y*yc = (a+b) * (A-B)

Not recommended:

i=i+1submitted +=1x = x * 2-1hypot2 = x * x + y * yc = (A + b) * (a)
    • Do not add a space near = when you are setting a keyword parameter or a default parameter value.
      Recommended:
def complex (Real, imag=0.0):    return Magic (R=real, I=imag)

Not recommended:

def complex (real, imag = 0.0):    return Magic (r = Real, I = imag)
    • Functional annotations should use the general rules of the colon, and add spaces on both sides when used. (Refer to the function note below to get more information)
      Recommended:
def munge (input:anystr): ... def munge ()-Anystr: ...

Not recommended:

def munge (input:anystr): ... def munge ()->posint: ...
    • When assigning a value to a parameter with a type comment, add a space on both sides (only for those parameters that have type notes and default values).
      Recommended:
def munge (sep:anystr = none): ... def munge (input:anystr, sep:anystr = None, limit=1000): ...

Not recommended:

def munge (Input:anystr=none): ... def munge (input:anystr, limit = 1000): ...
    • Compound statements (multiple statements in the same row) are generally not allowed.
      Recommended:
if foo = = ' blah ':    do_blah_thing () Do_one () Do_two () Do_three ()

Not recommended:

if foo = = ' blah ': do_blah_thing () Do_one (); Do_two (); Do_three ()
    • Although it is not a problem to put small blocks of code and if/for/while on the same line sometimes, it is not a good thing to do this with multiple lines of statements, but also to avoid long lines of code!
      Better not do this:
if foo = = ' blah ': do_blah_thing () for x in Lst:total + = Xwhile T < 10:t = delay ()

Not recommended:

if foo = = ' blah ': do_blah_thing () else:do_non_blah_thing () try:something () Finally:cleanup () Do_one (); Do_two (); Do_three (long, argument,                             list, like, this) if foo = = ' blah ': one (); both (); three ()

Python PEP8 encoding specification expressions and spaces in statements

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.