The Google Python style guide allows more variation (presumably to cover a large breadth of existing source). This guide has a tighter set of choices. So a Fuchsia Python file will also comply with the Google style guide, but a Google Python file might not comply with this guide. See refinements below for details.
Scripts invoked by the build
Scripts invoked by the build (GN or Ninja) should use
python3.8 in the shebang:
fx and the continuous integration infrastructure ensure that that
executable will be in the path during GN and Ninja invocations, so it's safe to
use in that context.
Scripts which are invoked directly should use
python in the shebang and be
compatible with both 2 and 3:
Developers working on Fuchsia modules may use various platforms. Some platforms include Python 2.x and not Python 3.x and vice versa. Until Python 3.x is included in the prominent development environments we support, we should support Python 2.x.
While Python 2.x is supported, test scripts on both versions. Python 2.7 will be supported by the Python team until January 1, 2020. When we drop Python 2.7 support will be influenced by, but not dictated by that support pledge from the Python team.
Multiple inheritance is strongly discouraged. This is for the same reason
listed in the
Google C++ style guide: risk of "diamond" inheritance
patterns, which are prone to confusion. If a case is found where avoiding
multiple inheritance is unreasonable, all classes involved must initially
inherit from the base class
object, which governs which multiple inheritance
scheme is used.
Use Unicode for Text
In scripts that support Python 2.x (see Python versions),
explicitly declare text strings as unicode and binary data as bytes, using
Python 3.x defaults to using Unicode for strings, so this guideline will be
removed when support for Python 2 is dropped.
Yes: a = u"Hello" # Unicode constant. b = unicode(foo) # Convert to Unicode. c = unichr(c) # Convert to Unicode. d = io.open("bar.txt").read() # Read text as Unicode.
No: a = "Hello" # Ambiguous (depends on Python version). b = str(foo) # Convert to ascii. c = chr(c) # Convert to ascii. d = open("bar.txt").read() # Read text as ascii.
The following refinements we make to the Google Python style guide are largely choices between variations. For example, if the style guide says you may do A, B, or C we may choose to favor B and avoid the other choices.
Avoid aligning with opening delimiter. Prefer instead to indent using fixed (4 space) indentation.
(See Indentation in the Google Python style guide for comparison.)
Avoid creating single line statements, even with
Yes: if foo: bar(foo)
No: if foo: bar(foo)
(See Statements in the Google Python style guide for comparison.)
In scripts that support Python 2.x (see Python versions), type annotations will not be used.
(See Type Annotations in the Google Python style guide for comparison.)
Prefer double quotes for strings (
"). Use single quotes when the declaration is
more readable with single quotes. For example,
'The cat said "Meow"' is more readable
"The cat said \\"Meow\\"".
(See Strings in the Google Python style guide for comparison.)
Be consistent within a large scope. Avoid displaying small pockets of consistency within Fuchsia. Being consistent within only a single file or directory is not consistency.
third_party, the intent is to follow the existing style for that project
or library. Look for a style guide within that library as appropriate.
(See Parting Words in the Google Python style guide.)