Example python code specification PEP8

 

"""
Documentation string docstring, a package, module, class, method, function-level annotations, can be accessed by members of the doc, annotation content in a pair of double quotes
Priority use English to write notes, all written in English is not good Chinese, English may lead to the use of force we can not read
1. All import as much as possible at the beginning of the file, in the docstring below, other variables defined above
2. Do not use from foo import *
3.import need to be grouped, a blank line between each, in order to maximize the use lexicographic each packet, packet sequence is:
    3.1 Standard Library
    3.2 third-party libraries
    package and module 3.3 of the project
4. Do not use a relative introduced implicit (implicit relative imports)
  Can be introduced using relative display (explicit relative imports), as from ..utils import parse
  Preferably use a full path to import (absolute imports)
5. The contents of the same package / module can write with
6. In order to avoid naming conflicts may arise, can be used on or as an import namespace

"""
import os
import sys
import db

from flask import Flask, render_template, jsonify
from collections import defaultdict as my_dict

# This is the right notes, behind # A space is required, as is a special case
#!/usr/bin/python

# Operation ends a space character, such as +, -, *, /, |, &, =
a = 1 + 2
b = 10 - 1
c = 1 * 2
d = 10 / 5
e = 10 | 20
f = 5 & 6


# In the parameter list of the space is not required at both ends =
def add(num1=1, num2=2):
    return num1 + num2


# Function and class top two blank lines
class MyClass(object):
    def my_function1(self):
        pass

    # Class between the method a blank line
    def my_function2(self):
        # Blank line between unrelated logic functions within a paragraph, do not overuse blank lines
        Logic # 1

        Logic # 2

        Logic # 3

        return


# Do not put multiple statements on one line, then; separated
add(1, 2)
add(3, 4)

((), {}, []) At both ends of the brackets in space is not required #
tmp = ('name1', 'name2')

# If / for / while statement, the statement is executed even if only one sentence, but also on a separate line
if True:
    print ( 'new line')

# Each line of code control in less than 80 characters
# Face of tricks to grab votes software, experts advise, although theoretically grab votes software manual refresh faster than the 12306 but has blocked many ports to grab votes and introduced the "official grab votes"
# Candidates function - that is, when I vote no, there will be 12306 the word "candidate" in the list of trips. Guests can choose trips on demand, seats do not, in successful payment
# Candidates after advance payment, if there is demand for match tickets, the system will automatically generate a payment order and return the difference; if not, the system is fully refundable prepayment. Well, this
# Alternate function of the market and who better to grab votes software use? If you use software to grab votes, and what routine to be careful?


# Use \ or () control line feed
def foo(tmp1='aa', tmp2='bb', tmp3='cc', tmp4='dd',
        tmp5 = 'Michael', tmp6 = 'ff', tmp7 = 'gg', tmp8 = 'HH'):
    pass


# Use meaningful, the English word or phrase, never use Hanyu Pinyin
address = 'suzhou'

# Package / module names do not appear -

# Do not use try / except, except behind the need to specify to capture the exception, bare except
# Will catch all exceptions, meaning that potential problems are hidden
# You can have multiple statements except to capture a variety of abnormalities were doing exception handling
age = '111'
try:
    age = int(age)
except(TypeError, ValueError):
    render_template()

# Use finally clause to deal with some finishing operations
try:
    db.session.commit()
except sqlalchemy.exc.SQLAlchemyError: # or more specific exception
    db.session.rollback()
finally:
    db.session.close()


# Display indicate the parent class, if not inherited from another class, it inherits from the object class
# Use super call the parent class
# Support multiple inheritance, that is, there are multiple parent classes, it is recommended to use Mixin
class MyClass(object):
    pass

  

Reference documents: https://zhuanlan.zhihu.com/p/33705005

Guess you like

Origin www.cnblogs.com/hester/p/12114668.html