Python style guide
The Fuchsia project follows theGoogle Python style guide,with a fewrefinements.
The Google Python style guide allows more variation (presumably to cover a largebreadth of existing source). This guide has a tighter set of choices. So aFuchsia Python file will also comply with the Google style guide, but a GooglePython file might not comply with this guide. Seerefinementsbelow for details.
Python versions
Scripts invoked by the build
Scripts invoked by the build (GN or Ninja) are executed with Python 3.8.
The build system ensures that all python scripts are executed by theinterpreter that is installed as part of a Fuchsia source checkout.
Other scripts
Scripts that are invoked directly should usepython in the shebang and becompatible with both 2 and 3:#!/usr/bin/env python.
Developers working on Fuchsia modules may use various platforms. Some platformsinclude Python 2 and not Python 3 and vice versa. Until Python 3 isincluded in the prominent development environments we support, we should supportPython 2.
While Python 2 is supported, test scripts on both versions.
Any policy change will be reflected in this document.
Multiple Inheritance
Multiple inheritance is strongly discouraged. This is for the same reasonlisted in theGoogle C++ style guide: risk of "diamond" inheritance patterns, which are prone to confusion. If a case is found where avoidingmultiple inheritance is unreasonable, all classes involved must initiallyinherit from the base classobject, which governs which multiple inheritancescheme is used.
Use Unicode for Text
In scripts that support Python 2.x (seePython versions),explicitly declare text strings as unicode and binary data as bytes, usingu"",unicode(),unichr() andb"",bytes(),byte() respectively.Python 3.x defaults to using Unicode for strings, so this guideline will beremoved 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.Refinements
The following refinements we make to the Google Python style guide are largelychoices 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.
Indentation
Avoid aligning with opening delimiter. Prefer instead to indent using fixed(4 space) indentation.
(SeeIndentation in the Google Python style guide for comparison.)
Statements
Avoid creating single line statements, even withif statements.
Yes:iffoo:bar(foo)No:iffoo:bar(foo)(SeeStatements in the Google Python style guide for comparison.)
Type annotations
In scripts that support Python 2 (seePython versions),type annotations will not be used.
(SeeType Annotations in the Google Python style guide for comparison.)
Strings
Prefer double quotes for strings ("). Use single quotes when the declaration ismore readable with single quotes. For example,'The cat said "Meow"' is more readablethan"The cat said \\"Meow\\"".
(SeeStrings in the Google Python style guide for comparison.)
Be consistent
Be consistent within a large scope. Avoid displaying small pockets of consistencywithin Fuchsia. Being consistent within only a single file or directory is notconsistency.
Withinthird_party, the intent is to follow the existing style for that projector library. Look for a style guide within that library as appropriate.
(SeeParting Words in the Google Python style guide.)
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-02-28 UTC.