不用使用带星号 * 的 import 语句,比如 from module import * ,因为这样很难确认被导入的函数和类。
不要在 函数或类的内部使用 import 导入模块。
【待翻译】
General
All files should have the license header at the beginning.
Unix line endings should be used.
Never use mixed line endings in the same file.
Use 4 spaces for a single level of indentation. Do not use tabs.
Remove trailing white spaces.
Keep files at less than 1000 lines in size.
Break a module into smaller modules, or into a package (a subdirectory with various modules), as necessary.
A big function can be broken into smaller functions.
A big class can be broken into smaller classes. Then one class can subclass the other ones in order to inherit their methods.
Python
Code formatting
In general, code should follow PEP 8 and PEP 257 (docstrings).
Maximum line length should be 80 characters.
Find ways to reduce nested blocks of code by using auxiliary variables, and functions that encapsulate the operations inside these blocks.
If you have more than 3 levels of indentation, this is a sign that the code should be refactored.
Use double quotes for "strings".
Imports
Imports should be at the beginning of the file, after the license header and module docstring, and they should be grouped in three types in order: standard library imports, third party imports, and then FreeCAD specific imports.
Import only one module per line.
Do not use asterisk imports, from module import *, as this makes it hard to validate imported functions and classes.
Do not import modules inside a function or class.
The import of modules that require the graphical interface, such as FreeCADGui, should be guarded by an if FreeCAD.GuiUp: test.
In general, the code should be structured in such a way that console-only functions are separate from their graphical interface implementations (GuiCommands).
functions_without_capitals() for functions and class methods.
Functions expected to return a value should indicate what is expected, so is_mesh_valid is a good name, but check_mesh is not a good name.
Class names, method names, and variables that are auxiliary, and not meant to be part of the public interface should start with an underscore like _MyInternalClass or _my_small_variable.
Python code formatting tools
Using a code editor that automatically checks compliance with PEP 8 is recommended.
For example, Spyder contains code checkers pylint, pyflakes, pycodestyle, jedi, sphinx, etc., that automatically test the code as you write it, and can provide documentation and hints on the used functions and classes in real time, even the ones that you have just written.
Compliance should be manually checked with flake8.