Python Coding Standards
Encoding Declaration
Default encoding is not ASCII. You can specify it using:
|
|
or
or
Code Layout
- Use four spaces for indentation; avoid using tabs, and never mix tabs with spaces.
- Limit line length to 79 characters. Use backslashes for line continuation, but prefer parentheses instead.
- Leave two blank lines between classes and top-level functions; leave one blank line between methods within a class.
- Module structure order: module description and docstring,
importstatements, global variables, constants, others. Withinimport, arrange in order: standard library, third-party, local modules. Avoid writing multiple imports on one line. - Add one space on both sides of operators. Omit spaces around assignment operators in default function arguments. Do not add spaces before closing brackets (
],},)). - Comments must be in English, preferably complete sentences. Start with a capital letter, end with a period (or other punctuation), followed by two spaces before the next sentence. For phrases, punctuation may be omitted.
- Always write docstrings for public modules, functions, classes, and methods. Non-public ones don’t require docstrings, but comments are acceptable (placed on the line immediately after
def).
Naming Conventions
- Avoid using single lowercase letter
lor uppercaseOas they can easily be confused. - Module names should be short and use all lowercase letters, optionally separated by underscores.
- Package names should be short and use all lowercase letters without underscores.
- Class names use
CapWords. Internal classes use_CapWords. - Exception names use
CapWordswith anErrorsuffix. - Global variables should ideally be limited to module scope (like
staticin C). Two ways to achieve this: use__all__mechanism or prefix with an underscore. - Function names use all lowercase letters, optionally separated by underscores.
- Constant names use all uppercase letters, optionally separated by underscores.
- Class attributes (methods and variables) use all lowercase letters, optionally separated by underscores.
- Class attributes have three scopes: public, non-public, and subclass API—similar to
public,private,protectedin C++. Prefix non-public attributes with a single underscore. - If a class attribute conflicts with a keyword, append an underscore. Avoid abbreviations or other workarounds.
- To prevent naming conflicts with subclasses, prefix certain class attributes with double underscores. For example, in class
Foo, define__a; access viaFoo._Foo__ato avoid ambiguity. If a subclass also usesFoo, this protection fails. - The first parameter of instance methods must be
self. The first parameter of static methods must becls.
Code Checking Tools
|
|
After installing flake8 successfully, open VSCode, go to File → Preferences → Settings, and add "python.linting.flake8Enabled": true in settings.json.
Auto Formatting Tool
|
|
After installing yapf successfully, open VSCode, go to File → Preferences → Settings, and add "python.formatting.provider": "yapf" in settings.json.
Project Development Notes:
When starting a new project, keep the following in mind:
README.md: Write your project overview, quick start guide, etc. Althoughdistutilsrequires no extension, GitHub renders.mdfiles directly as HTML.ChangeLog.txt: Store version change logs with a consistent format. Refer toweb.py’sChangeLog.txtfor examples.LICENSE.txt: Include the license used by your project. Do not create custom licenses.requirements.txt: List dependencies on separate lines. This allowspip installto automatically install them.setup.py: Installation script — detailed later.docs: Store project documentation such as high-level design, detailed design, maintenance documents, and auto-generatedpydoccontent. Strongly recommend using Markdown format.src: Place main module code here. Avoid placing module directories directly in the root directory. Module paths can be specified insetup.py.tests: Store all unit tests and performance test scripts. Test files must start withtest_so thatdistutilswill automatically include them in packaging. Run tests using:
python -m unittest discover -s ./ -p 'test_*.py' -v
References:
https://www.python.org/dev/peps/pep-0008/ https://wiki.woodpecker.org.cn/moin/PythonCodingRule