You can write Python for as long as you like, instantly relieving the pain of centuries of suffering from C, C ++, and Java, but Python says “Please use PEP 8”.

PEP 8 code specification: www.python.org/dev/peps/pe…

The Python Enhancement Proposal no. 8, also known as PEP 8, is a style guide for Python code formats. It provides guidelines for the best code styles. Here is a partial extract of the important rules, see the links above for the full content.

The blank space

  • Use Spaces instead of indents, not tabs
  • Indent each layer with 4 Spaces
  • Each line contains a maximum of 79 characters
  • Multi-line expression with 4 Spaces for the first line and 8 Spaces for each line
  • Functions and classes are separated by two blank lines
  • The methods in the class are separated by blank lines
  • Assign an equal sign with a spacea = "bcde"

Indentation demo:

# Aligned with opening delimiter. foo = long_function_name(var_one, var_two, var_three, var_four) # Add 4 spaces (an extra level of indentation) to distinguish arguments from the rest. def long_function_name(  var_one, var_two, var_three, var_four): print(var_one) # Hanging indents should add a level. foo = long_function_name( var_one, var_two, var_three, var_four)Copy the code

Code split multiple lines:

Conditional statement if

# No extra indentation.
if (this_is_one_thing and
    that_is_another_thing):
    do_something()

# Add a comment, which will provide some distinction in editors
# supporting syntax highlighting.
if (this_is_one_thing and
    that_is_another_thing):
    # Since both conditions are true, we can frobnicate.
    do_something()

# Add some extra indentation on the conditional continuation line.
if (this_is_one_thing
        and that_is_another_thing):
    do_something()
Copy the code

List and function multiarguments:

my_list = [
    1, 2, 3,
    4, 5, 6,
    ]
result = some_function_that_takes_arguments(
    'a', 'b', 'c',
    'd', 'e', 'f',
    )
Copy the code

Code Branch:

with open('/path/to/some/file/you/want/to/read') as file_1, \
     open('/path/to/some/file/being/written', 'w') as file_2:
    file_2.write(file_1.read())
Copy the code

Operator multiple lines:

# easy to match operators with operands
income = (gross_wages
          + taxable_interest
          + (dividends - qualified_dividends)
          - ira_deduction
          - student_loan_interest)
Copy the code

(Source: Hogwarts Testing Institute)

More technical articles can be found at qrcode.testing-studio.com/f?from=juej…