2018-02-06 04:51:47 +08:00
<!--
AUTHORS:
Prefer only GitHub-flavored Markdown in external text.
See README.md for details.
-->
# Google Python Style Guide
2020-08-08 01:11:23 +08:00
<!-- markdown="1" is required for GitHub Pages to render the TOC properly. -->
< details markdown = "1" >
2020-06-30 04:39:32 +08:00
< summary > Table of Contents< / summary >
- [1 Background ](#s1-background )
- [2 Python Language Rules ](#s2-python-language-rules )
* [2.1 Lint ](#s2.1-lint )
* [2.2 Imports ](#s2.2-imports )
* [2.3 Packages ](#s2.3-packages )
* [2.4 Exceptions ](#s2.4-exceptions )
* [2.5 Global variables ](#s2.5-global-variables )
* [2.6 Nested/Local/Inner Classes and Functions ](#s2.6-nested )
* [2.7 Comprehensions & Generator Expressions ](#s2.7-comprehensions )
* [2.8 Default Iterators and Operators ](#s2.8-default-iterators-and-operators )
* [2.9 Generators ](#s2.9-generators )
* [2.10 Lambda Functions ](#s2.10-lambda-functions )
* [2.11 Conditional Expressions ](#s2.11-conditional-expressions )
* [2.12 Default Argument Values ](#s2.12-default-argument-values )
* [2.13 Properties ](#s2.13-properties )
* [2.14 True/False Evaluations ](#s2.14-truefalse-evaluations )
* [2.16 Lexical Scoping ](#s2.16-lexical-scoping )
* [2.17 Function and Method Decorators ](#s2.17-function-and-method-decorators )
* [2.18 Threading ](#s2.18-threading )
* [2.19 Power Features ](#s2.19-power-features )
2021-07-21 02:02:30 +08:00
* [2.20 Modern Python: from \_\_future\_\_ imports ](#s2.20-modern-python )
2020-06-30 04:39:32 +08:00
* [2.21 Type Annotated Code ](#s2.21-type-annotated-code )
- [3 Python Style Rules ](#s3-python-style-rules )
* [3.1 Semicolons ](#s3.1-semicolons )
* [3.2 Line length ](#s3.2-line-length )
* [3.3 Parentheses ](#s3.3-parentheses )
* [3.4 Indentation ](#s3.4-indentation )
2021-07-15 05:01:21 +08:00
+ [3.4.1 Trailing commas in sequences of items? ](#s3.4.1-trailing-commas )
2020-06-30 04:39:32 +08:00
* [3.5 Blank Lines ](#s3.5-blank-lines )
* [3.6 Whitespace ](#s3.6-whitespace )
* [3.7 Shebang Line ](#s3.7-shebang-line )
* [3.8 Comments and Docstrings ](#s3.8-comments-and-docstrings )
+ [3.8.1 Docstrings ](#s3.8.1-comments-in-doc-strings )
+ [3.8.2 Modules ](#s3.8.2-comments-in-modules )
+ [3.8.3 Functions and Methods ](#s3.8.3-functions-and-methods )
+ [3.8.4 Classes ](#s3.8.4-comments-in-classes )
+ [3.8.5 Block and Inline Comments ](#s3.8.5-block-and-inline-comments )
+ [3.8.6 Punctuation, Spelling, and Grammar ](#s3.8.6-punctuation-spelling-and-grammar )
* [3.10 Strings ](#s3.10-strings )
2020-12-16 06:31:04 +08:00
+ [3.10.1 Logging ](#s3.10.1-logging )
+ [3.10.2 Error Messages ](#s3.10.2-error-messages )
2021-07-15 05:01:21 +08:00
* [3.11 Files, Sockets, and similar Stateful Resources ](#s3.11-files-sockets-closeables )
2020-06-30 04:39:32 +08:00
* [3.12 TODO Comments ](#s3.12-todo-comments )
* [3.13 Imports formatting ](#s3.13-imports-formatting )
* [3.14 Statements ](#s3.14-statements )
* [3.15 Accessors ](#s3.15-accessors )
* [3.16 Naming ](#s3.16-naming )
+ [3.16.1 Names to Avoid ](#s3.16.1-names-to-avoid )
+ [3.16.2 Naming Conventions ](#s3.16.2-naming-conventions )
+ [3.16.3 File Naming ](#s3.16.3-file-naming )
+ [3.16.4 Guidelines derived from Guido's Recommendations ](#s3.16.4-guidelines-derived-from-guidos-recommendations )
* [3.17 Main ](#s3.17-main )
* [3.18 Function length ](#s3.18-function-length )
* [3.19 Type Annotations ](#s3.19-type-annotations )
+ [3.19.1 General Rules ](#s3.19.1-general-rules )
+ [3.19.2 Line Breaking ](#s3.19.2-line-breaking )
+ [3.19.3 Forward Declarations ](#s3.19.3-forward-declarations )
+ [3.19.4 Default Values ](#s3.19.4-default-values )
+ [3.19.5 NoneType ](#s3.19.5-nonetype )
+ [3.19.6 Type Aliases ](#s3.19.6-type-aliases )
+ [3.19.7 Ignoring Types ](#s3.19.7-ignoring-types )
+ [3.19.8 Typing Variables ](#s3.19.8-typing-variables )
+ [3.19.9 Tuples vs Lists ](#s3.19.9-tuples-vs-lists )
+ [3.19.10 TypeVars ](#s3.19.10-typevars )
+ [3.19.11 String types ](#s3.19.11-string-types )
+ [3.19.12 Imports For Typing ](#s3.19.12-imports-for-typing )
+ [3.19.13 Conditional Imports ](#s3.19.13-conditional-imports )
+ [3.19.14 Circular Dependencies ](#s3.19.14-circular-dependencies )
+ [3.19.15 Generics ](#s3.19.15-generics )
+ [3.19.16 Build Dependencies ](#s3.19.16-build-dependencies )
- [4 Parting Words ](#4-parting-words )
< / details >
< a id = "s1-background" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "1-background" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "background" > < / a >
2019-10-10 15:40:18 +08:00
## 1 Background
2018-02-06 04:51:47 +08:00
Python is the main dynamic language used at Google. This style guide is a list
of *dos and don'ts* for Python programs.
2020-06-30 04:39:32 +08:00
To help you format code correctly, we've created a [settings file for Vim ](google_python_style.vim ). For Emacs, the default settings should be fine.
2018-02-06 04:51:47 +08:00
Many teams use the [yapf ](https://github.com/google/yapf/ )
auto-formatter to avoid arguing over formatting.
< a id = "s2-python-language-rules" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2-python-language-rules" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "python-language-rules" > < / a >
2019-10-10 15:40:18 +08:00
## 2 Python Language Rules
2018-02-06 04:51:47 +08:00
< a id = "s2.1-lint" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "21-lint" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "lint" > < / a >
2019-10-10 15:40:18 +08:00
### 2.1 Lint
2018-02-06 04:51:47 +08:00
2020-09-22 01:50:54 +08:00
Run `pylint` over your code using this [pylintrc ](https://google.github.io/styleguide/pylintrc ).
2018-02-06 04:51:47 +08:00
< a id = "s2.1.1-definition" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "211-definition" > < / a >
< a id = "lint-definition" > < / a >
#### 2.1.1 Definition
2018-02-06 04:51:47 +08:00
2020-08-07 03:30:10 +08:00
`pylint`
is a tool for finding bugs and style problems in Python source code. It finds
problems that are typically caught by a compiler for less dynamic languages like
C and C++. Because of the dynamic nature of Python, some
2018-02-06 04:51:47 +08:00
warnings may be incorrect; however, spurious warnings should be fairly
infrequent.
< a id = "s2.1.2-pros" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "212-pros" > < / a >
< a id = "lint-pros" > < / a >
#### 2.1.2 Pros
2018-02-06 04:51:47 +08:00
Catches easy-to-miss errors like typos, using-vars-before-assignment, etc.
< a id = "s2.1.3-cons" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "213-cons" > < / a >
< a id = "lint-cons" > < / a >
#### 2.1.3 Cons
2018-02-06 04:51:47 +08:00
2020-08-07 03:30:10 +08:00
`pylint`
2020-12-16 06:31:04 +08:00
isn't perfect. To take advantage of it, sometimes we'll need to write around it,
suppress its warnings or fix it.
2018-02-06 04:51:47 +08:00
< a id = "s2.1.4-decision" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "214-decision" > < / a >
< a id = "lint-decision" > < / a >
#### 2.1.4 Decision
2018-02-06 04:51:47 +08:00
2020-08-07 03:30:10 +08:00
Make sure you run
`pylint`
on your code.
2018-02-06 04:51:47 +08:00
Suppress warnings if they are inappropriate so that other issues are not hidden.
To suppress warnings, you can set a line-level comment:
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
dict = 'something awful' # Bad Idea... pylint: disable=redefined-builtin
```
2020-08-07 03:30:10 +08:00
`pylint`
warnings are each identified by symbolic name (`empty-docstring`)
2018-02-06 04:51:47 +08:00
Google-specific warnings start with `g-` .
If the reason for the suppression is not clear from the symbolic name, add an
explanation.
Suppressing in this way has the advantage that we can easily search for
suppressions and revisit them.
2020-08-07 03:30:10 +08:00
You can get a list of
`pylint`
warnings by doing:
2018-02-06 04:51:47 +08:00
```shell
pylint --list-msgs
```
To get more information on a particular message, use:
```shell
pylint --help-msg=C6409
```
Prefer `pylint: disable` to the deprecated older form `pylint: disable-msg` .
Unused argument warnings can be suppressed by deleting the variables at the
beginning of the function. Always include a comment explaining why you are
deleting it. "Unused." is sufficient. For example:
2018-11-19 10:21:51 +08:00
```python
2021-07-15 05:01:21 +08:00
def viking_cafe_order(spam: str, beans: str, eggs: Optional[str] = None) -> str:
2018-02-06 04:51:47 +08:00
del beans, eggs # Unused by vikings.
return spam + spam + spam
```
Other common forms of suppressing this warning include using '`_`' as the
2020-06-30 04:39:32 +08:00
identifier for the unused argument or prefixing the argument name with
2018-02-06 04:51:47 +08:00
'`unused_`', or assigning them to '`_`'. These forms are allowed but no longer
2020-06-30 04:39:32 +08:00
encouraged. These break callers that pass arguments by name and do not enforce
that the arguments are actually unused.
2018-02-06 04:51:47 +08:00
< a id = "s2.2-imports" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "22-imports" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "imports" > < / a >
2019-10-10 15:40:18 +08:00
### 2.2 Imports
2018-02-06 04:51:47 +08:00
2018-12-06 06:40:50 +08:00
Use `import` statements for packages and modules only, not for individual
2022-05-26 03:15:47 +08:00
classes or functions.
2018-02-06 04:51:47 +08:00
< a id = "s2.2.1-definition" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "221-definition" > < / a >
< a id = "imports-definition" > < / a >
#### 2.2.1 Definition
2018-02-06 04:51:47 +08:00
Reusability mechanism for sharing code from one module to another.
< a id = "s2.2.2-pros" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "222-pros" > < / a >
< a id = "imports-pros" > < / a >
#### 2.2.2 Pros
2018-02-06 04:51:47 +08:00
The namespace management convention is simple. The source of each identifier is
indicated in a consistent way; `x.Obj` says that object `Obj` is defined in
module `x` .
< a id = "s2.2.3-cons" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "223-cons" > < / a >
< a id = "imports-cons" > < / a >
#### 2.2.3 Cons
2018-02-06 04:51:47 +08:00
Module names can still collide. Some module names are inconveniently long.
< a id = "s2.2.4-decision" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "224-decision" > < / a >
< a id = "imports-decision" > < / a >
#### 2.2.4 Decision
2018-02-06 04:51:47 +08:00
2020-08-07 03:30:10 +08:00
* Use `import x` for importing packages and modules.
* Use `from x import y` where `x` is the package prefix and `y` is the module
name with no prefix.
2022-04-12 01:17:08 +08:00
* Use `from x import y as z` if two modules named `y` are to be imported, if
`y` conflicts with a top-level name defined in the current module, or if `y`
is an inconveniently long name.
2020-08-07 03:30:10 +08:00
* Use `import y as z` only when `z` is a standard abbreviation (e.g., `np` for
`numpy` ).
2018-02-06 04:51:47 +08:00
For example the module `sound.effects.echo` may be imported as follows:
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
from sound.effects import echo
...
echo.EchoFilter(input, output, delay=0.7, atten=4)
```
Do not use relative names in imports. Even if the module is in the same package,
use the full package name. This helps prevent unintentionally importing a
package twice.
2022-05-26 03:15:47 +08:00
< a id = "imports-exemptions" > < / a >
##### 2.2.4.1 Exemptions
Exemptions from this rule:
2022-06-08 07:37:30 +08:00
* Symbols from the following modules and used to support static analysis and
type checking:
* [`typing` module ](#typing-imports )
* [`collections.abc` module ](#typing-imports )
* [`typing_extensions` module ](https://github.com/python/typing_extensions/blob/main/README.md )
2022-05-26 03:15:47 +08:00
* Redirects from the
[six.moves module ](https://six.readthedocs.io/#module-six.moves ).
2018-02-06 04:51:47 +08:00
< a id = "s2.3-packages" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "23-packages" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "packages" > < / a >
2019-10-10 15:40:18 +08:00
### 2.3 Packages
2018-02-06 04:51:47 +08:00
Import each module using the full pathname location of the module.
< a id = "s2.3.1-pros" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "231-pros" > < / a >
< a id = "packages-pros" > < / a >
#### 2.3.1 Pros
2018-02-06 04:51:47 +08:00
2018-11-29 02:15:08 +08:00
Avoids conflicts in module names or incorrect imports due to the module search
2020-08-07 03:30:10 +08:00
path not being what the author expected. Makes it easier to find modules.
2018-02-06 04:51:47 +08:00
2019-10-10 15:40:18 +08:00
< a id = "s2.3.2-cons" > < / a >
< a id = "232-cons" > < / a >
< a id = "packages-cons" > < / a >
#### 2.3.2 Cons
2018-02-06 04:51:47 +08:00
Makes it harder to deploy code because you have to replicate the package
2020-08-07 03:30:10 +08:00
hierarchy. Not really a problem with modern deployment mechanisms.
2018-02-06 04:51:47 +08:00
< a id = "s2.3.3-decision" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "233-decision" > < / a >
< a id = "packages-decision" > < / a >
#### 2.3.3 Decision
2018-02-06 04:51:47 +08:00
All new code should import each module by its full package name.
Imports should be as follows:
2018-11-19 10:21:51 +08:00
```python
2021-09-13 13:42:39 +08:00
Yes:
# Reference absl.flags in code with the complete name (verbose).
import absl.flags
from doctor.who import jodie
2018-11-29 02:15:08 +08:00
2022-04-12 01:17:08 +08:00
_FOO = absl.flags.DEFINE_string(...)
2018-11-29 02:15:08 +08:00
```
2018-02-06 04:51:47 +08:00
2018-11-29 02:15:08 +08:00
```python
2021-09-13 13:42:39 +08:00
Yes:
# Reference flags in code with just the module name (common).
from absl import flags
from doctor.who import jodie
2018-11-29 02:15:08 +08:00
2022-04-12 01:17:08 +08:00
_FOO = flags.DEFINE_string(...)
2018-02-06 04:51:47 +08:00
```
2021-12-08 02:42:12 +08:00
*(assume this file lives in `doctor/who/` where `jodie.py` also exists)*
2018-11-29 02:15:08 +08:00
```python
2021-09-13 13:42:39 +08:00
No:
# Unclear what module the author wanted and what will be imported. The actual
# import behavior depends on external factors controlling sys.path.
# Which possible jodie module did the author intend to import?
import jodie
2018-11-29 02:15:08 +08:00
```
The directory the main binary is located in should not be assumed to be in
2020-08-07 03:30:10 +08:00
`sys.path` despite that happening in some environments. This being the case,
2018-11-29 02:15:08 +08:00
code should assume that `import jodie` refers to a third party or top level
package named `jodie` , not a local `jodie.py` .
2019-10-10 15:40:18 +08:00
2018-02-06 04:51:47 +08:00
< a id = "s2.4-exceptions" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "24-exceptions" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "exceptions" > < / a >
2019-10-10 15:40:18 +08:00
### 2.4 Exceptions
2018-02-06 04:51:47 +08:00
Exceptions are allowed but must be used carefully.
< a id = "s2.4.1-definition" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "241-definition" > < / a >
< a id = "exceptions-definition" > < / a >
#### 2.4.1 Definition
2018-02-06 04:51:47 +08:00
2020-12-16 06:31:04 +08:00
Exceptions are a means of breaking out of normal control flow to handle errors
or other exceptional conditions.
2018-02-06 04:51:47 +08:00
< a id = "s2.4.2-pros" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "242-pros" > < / a >
< a id = "exceptions-pros" > < / a >
#### 2.4.2 Pros
2018-02-06 04:51:47 +08:00
The control flow of normal operation code is not cluttered by error-handling
code. It also allows the control flow to skip multiple frames when a certain
condition occurs, e.g., returning from N nested functions in one step instead of
2020-12-16 06:31:04 +08:00
having to plumb error codes through.
2018-02-06 04:51:47 +08:00
< a id = "s2.4.3-cons" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "243-cons" > < / a >
< a id = "exceptions-cons" > < / a >
#### 2.4.3 Cons
2018-02-06 04:51:47 +08:00
May cause the control flow to be confusing. Easy to miss error cases when making
library calls.
< a id = "s2.4.4-decision" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "244-decision" > < / a >
< a id = "exceptions-decision" > < / a >
#### 2.4.4 Decision
2018-02-06 04:51:47 +08:00
Exceptions must follow certain conditions:
- Make use of built-in exception classes when it makes sense. For example,
2019-10-10 15:40:18 +08:00
raise a `ValueError` to indicate a programming mistake like a violated
precondition (such as if you were passed a negative number but required a
positive one). Do not use `assert` statements for validating argument values
of a public API. `assert` is used to ensure internal correctness, not to
enforce correct usage nor to indicate that some unexpected event occurred.
If an exception is desired in the latter cases, use a raise statement. For
example:
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes:
2021-07-15 05:01:21 +08:00
def connect_to_next_port(self, minimum: int) -> int:
2018-11-15 23:47:37 +08:00
"""Connects to the next available port.
Args:
minimum: A port value greater or equal to 1024.
2019-10-10 15:40:18 +08:00
2018-11-15 23:47:37 +08:00
Returns:
The new minimum port.
2019-10-10 15:40:18 +08:00
Raises:
ConnectionError: If no available port is found.
2018-11-15 23:47:37 +08:00
"""
if minimum < 1024:
2019-10-10 15:40:18 +08:00
# Note that this raising of ValueError is not mentioned in the doc
# string's "Raises:" section because it is not appropriate to
# guarantee this specific behavioral reaction to API misuse.
2020-06-30 04:39:32 +08:00
raise ValueError(f'Min. port must be at least 1024, not {minimum}.')
2018-11-15 23:47:37 +08:00
port = self._find_next_open_port(minimum)
2022-04-12 01:17:08 +08:00
if port is None:
2020-06-30 04:39:32 +08:00
raise ConnectionError(
f'Could not connect to service on port {minimum} or higher.')
assert port >= minimum, (
f'Unexpected port {port} when minimum was {minimum}.')
2018-02-06 04:51:47 +08:00
return port
```
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
No:
2021-07-15 05:01:21 +08:00
def connect_to_next_port(self, minimum: int) -> int:
2018-11-15 23:47:37 +08:00
"""Connects to the next available port.
Args:
minimum: A port value greater or equal to 1024.
2019-10-10 15:40:18 +08:00
2018-11-15 23:47:37 +08:00
Returns:
The new minimum port.
"""
assert minimum >= 1024, 'Minimum port must be at least 1024.'
port = self._find_next_open_port(minimum)
2018-02-06 04:51:47 +08:00
assert port is not None
return port
```
2021-12-08 02:42:12 +08:00
2018-02-06 04:51:47 +08:00
- Libraries or packages may define their own exceptions. When doing so they
must inherit from an existing exception class. Exception names should end in
2021-09-13 13:42:39 +08:00
`Error` and should not introduce repetition (`foo.FooError`).
2018-02-06 04:51:47 +08:00
- Never use catch-all `except:` statements, or catch `Exception` or
2019-10-10 15:40:18 +08:00
`StandardError` , unless you are
- re-raising the exception, or
- creating an isolation point in the program where exceptions are not
propagated but are recorded and suppressed instead, such as protecting a
thread from crashing by guarding its outermost block.
Python is very tolerant in this regard and `except:` will really catch
everything including misspelled names, sys.exit() calls, Ctrl+C interrupts,
unittest failures and all kinds of other exceptions that you simply don't
want to catch.
2018-02-06 04:51:47 +08:00
- Minimize the amount of code in a `try` /`except` block. The larger the body
of the `try` , the more likely that an exception will be raised by a line of
code that you didn't expect to raise an exception. In those cases, the
`try` /`except` block hides a real error.
- Use the `finally` clause to execute code whether or not an exception is
raised in the `try` block. This is often useful for cleanup, i.e., closing a
file.
< a id = "s2.5-global-variables" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "25-global-variables" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "global-variables" > < / a >
2019-10-10 15:40:18 +08:00
### 2.5 Global variables
2018-02-06 04:51:47 +08:00
Avoid global variables.
< a id = "s2.5.1-definition" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "251-definition" > < / a >
< a id = "global-variables-definition" > < / a >
#### 2.5.1 Definition
2018-02-06 04:51:47 +08:00
Variables that are declared at the module level or as class attributes.
< a id = "s2.5.2-pros" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "252-pros" > < / a >
< a id = "global-variables-pros" > < / a >
#### 2.5.2 Pros
2018-02-06 04:51:47 +08:00
Occasionally useful.
< a id = "s2.5.3-cons" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "253-cons" > < / a >
< a id = "global-variables-cons" > < / a >
#### 2.5.3 Cons
2018-02-06 04:51:47 +08:00
Has the potential to change module behavior during the import, because
assignments to global variables are done when the module is first imported.
< a id = "s2.5.4-decision" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "254-decision" > < / a >
< a id = "global-variables-decision" > < / a >
#### 2.5.4 Decision
2018-02-06 04:51:47 +08:00
Avoid global variables.
2022-04-12 01:17:08 +08:00
If needed, global variables should be declared at the module level and made
internal to the module by prepending an `_` to the name. External access to
global variables must be done through public module-level functions. See
[Naming ](#s3.16-naming ) below.
2018-02-06 04:51:47 +08:00
2022-04-12 01:17:08 +08:00
While module-level constants are technically variables, they are permitted and
encouraged. For example: `MAX_HOLY_HANDGRENADE_COUNT = 3` . Constants must be
named using all caps with underscores. See [Naming ](#s3.16-naming ) below.
2018-02-06 04:51:47 +08:00
< a id = "s2.6-nested" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "26-nested" > < / a >
< a id = "nested-classes-functions" > < / a >
### 2.6 Nested/Local/Inner Classes and Functions
2018-02-06 04:51:47 +08:00
Nested local functions or classes are fine when used to close over a local
variable. Inner classes are fine.
< a id = "s2.6.1-definition" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "261-definition" > < / a >
< a id = "nested-classes-functions-definition" > < / a >
#### 2.6.1 Definition
2018-02-06 04:51:47 +08:00
A class can be defined inside of a method, function, or class. A function can be
defined inside a method or function. Nested functions have read-only access to
variables defined in enclosing scopes.
< a id = "s2.6.2-pros" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "262-pros" > < / a >
< a id = "nested-classes-functions-pros" > < / a >
#### 2.6.2 Pros
2018-02-06 04:51:47 +08:00
Allows definition of utility classes and functions that are only used inside of
a very limited scope. Very
[ADT ](http://www.google.com/url?sa=D&q=http://en.wikipedia.org/wiki/Abstract_data_type )-y.
2018-11-15 23:47:37 +08:00
Commonly used for implementing decorators.
2018-02-06 04:51:47 +08:00
< a id = "s2.6.3-cons" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "263-cons" > < / a >
< a id = "nested-classes-functions-cons" > < / a >
#### 2.6.3 Cons
2018-02-06 04:51:47 +08:00
2020-12-16 06:31:04 +08:00
Nested functions and classes cannot be directly tested. Nesting can make the
outer function longer and less readable.
2018-02-06 04:51:47 +08:00
< a id = "s2.6.4-decision" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "264-decision" > < / a >
< a id = "nested-classes-functions-decision" > < / a >
#### 2.6.4 Decision
2018-02-06 04:51:47 +08:00
2018-11-15 23:47:37 +08:00
They are fine with some caveats. Avoid nested functions or classes except when
2021-05-07 06:53:45 +08:00
closing over a local value other than `self` or `cls` . Do not nest a function
just to hide it from users of a module. Instead, prefix its name with an \_ at
the module level so that it can still be accessed by tests.
2018-02-06 04:51:47 +08:00
2020-06-30 04:39:32 +08:00
< a id = "s2.7-comprehensions" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "s2.7-list_comprehensions" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "27-list_comprehensions" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "list_comprehensions" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "list-comprehensions" > < / a >
< a id = "comprehensions" > < / a >
### 2.7 Comprehensions & Generator Expressions
2018-02-06 04:51:47 +08:00
Okay to use for simple cases.
< a id = "s2.7.1-definition" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "271-definition" > < / a >
< a id = "comprehensions-definition" > < / a >
#### 2.7.1 Definition
2018-02-06 04:51:47 +08:00
List, Dict, and Set comprehensions as well as generator expressions provide a
concise and efficient way to create container types and iterators without
resorting to the use of traditional loops, `map()` , `filter()` , or `lambda` .
< a id = "s2.7.2-pros" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "272-pros" > < / a >
< a id = "comprehensions-pros" > < / a >
#### 2.7.2 Pros
2018-02-06 04:51:47 +08:00
Simple comprehensions can be clearer and simpler than other dict, list, or set
creation techniques. Generator expressions can be very efficient, since they
avoid the creation of a list entirely.
< a id = "s2.7.3-cons" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "273-cons" > < / a >
< a id = "comprehensions-cons" > < / a >
#### 2.7.3 Cons
2018-02-06 04:51:47 +08:00
Complicated comprehensions or generator expressions can be hard to read.
< a id = "s2.7.4-decision" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "274-decision" > < / a >
< a id = "comprehensions-decision" > < / a >
#### 2.7.4 Decision
2018-02-06 04:51:47 +08:00
Okay to use for simple cases. Each portion must fit on one line: mapping
expression, `for` clause, filter expression. Multiple `for` clauses or filter
expressions are not permitted. Use loops instead when things get more
complicated.
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes:
2018-11-15 23:47:37 +08:00
result = [mapping_expr for value in iterable if filter_expr]
result = [{'key': value} for value in iterable
if a_long_filter_expression(value)]
result = [complicated_transform(x)
for x in iterable if predicate(x)]
descriptive_name = [
transform({'key': key, 'value': value}, color='black')
for key, value in generate_iterable(some_input)
if complicated_condition_is_met(key, value)
]
2018-02-06 04:51:47 +08:00
result = []
for x in range(10):
for y in range(5):
if x * y > 10:
result.append((x, y))
2018-11-15 23:47:37 +08:00
return {x: complicated_transform(x)
2018-02-06 04:51:47 +08:00
for x in long_generator_function(parameter)
2018-11-15 23:47:37 +08:00
if x is not None}
2018-02-06 04:51:47 +08:00
2018-11-15 23:47:37 +08:00
squares_generator = (x**2 for x in range(10))
unique_names = {user.name for user in users if user is not None}
2018-02-06 04:51:47 +08:00
eat(jelly_bean for jelly_bean in jelly_beans
if jelly_bean.color == 'black')
```
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
No:
2018-11-15 23:47:37 +08:00
result = [complicated_transform(
x, some_argument=x+1)
for x in iterable if predicate(x)]
2018-02-06 04:51:47 +08:00
result = [(x, y) for x in range(10) for y in range(5) if x * y > 10]
return ((x, y, z)
2019-12-31 05:45:23 +08:00
for x in range(5)
for y in range(5)
2018-02-06 04:51:47 +08:00
if x != y
2019-12-31 05:45:23 +08:00
for z in range(5)
2018-02-06 04:51:47 +08:00
if y != z)
```
< a id = "s2.8-default-iterators-and-operators" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "default-iterators-operators" > < / a >
### 2.8 Default Iterators and Operators
2018-02-06 04:51:47 +08:00
Use default iterators and operators for types that support them, like lists,
dictionaries, and files.
< a id = "s2.8.1-definition" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "281-definition" > < / a >
< a id = "default-iterators-operators-definition" > < / a >
#### 2.8.1 Definition
2018-02-06 04:51:47 +08:00
Container types, like dictionaries and lists, define default iterators and
membership test operators ("in" and "not in").
< a id = "s2.8.2-pros" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "282-pros" > < / a >
< a id = "default-iterators-operators-pros" > < / a >
#### 2.8.2 Pros
2018-02-06 04:51:47 +08:00
The default iterators and operators are simple and efficient. They express the
operation directly, without extra method calls. A function that uses default
operators is generic. It can be used with any type that supports the operation.
< a id = "s2.8.3-cons" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "283-cons" > < / a >
< a id = "default-iterators-operators-cons" > < / a >
#### 2.8.3 Cons
2018-02-06 04:51:47 +08:00
2020-06-30 04:39:32 +08:00
You can't tell the type of objects by reading the method names (e.g. `has_key()`
2018-02-06 04:51:47 +08:00
means a dictionary). This is also an advantage.
< a id = "s2.8.4-decision" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "284-decision" > < / a >
< a id = "default-iterators-operators-decision" > < / a >
#### 2.8.4 Decision
2018-02-06 04:51:47 +08:00
Use default iterators and operators for types that support them, like lists,
dictionaries, and files. The built-in types define iterator methods, too. Prefer
these methods to methods that return lists, except that you should not mutate a
2020-12-16 06:31:04 +08:00
container while iterating over it.
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes: for key in adict: ...
if key not in adict: ...
if obj in alist: ...
for line in afile: ...
2018-12-06 06:40:50 +08:00
for k, v in adict.items(): ...
2018-02-06 04:51:47 +08:00
```
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
No: for key in adict.keys(): ...
if not adict.has_key(key): ...
for line in afile.readlines(): ...
```
< a id = "s2.9-generators" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "29-generators" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "generators" > < / a >
2019-10-10 15:40:18 +08:00
### 2.9 Generators
2018-02-06 04:51:47 +08:00
Use generators as needed.
2018-03-22 12:52:20 +08:00
< a id = "s2.9.1-definition" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "291-definition" > < / a >
< a id = "generators-definition" > < / a >
2022-05-26 03:15:47 +08:00
#### 2.9.1 Definition
2018-02-06 04:51:47 +08:00
A generator function returns an iterator that yields a value each time it
executes a yield statement. After it yields a value, the runtime state of the
generator function is suspended until the next value is needed.
2018-03-22 12:52:20 +08:00
< a id = "s2.9.2-pros" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "292-pros" > < / a >
< a id = "generators-pros" > < / a >
#### 2.9.2 Pros
2018-02-06 04:51:47 +08:00
Simpler code, because the state of local variables and control flow are
preserved for each call. A generator uses less memory than a function that
creates an entire list of values at once.
2018-03-22 12:52:20 +08:00
< a id = "s2.9.3-cons" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "293-cons" > < / a >
< a id = "generators-cons" > < / a >
#### 2.9.3 Cons
2018-02-06 04:51:47 +08:00
2022-05-26 03:15:47 +08:00
Local variables in the generator will not be garbage collected until the
generator is either consumed to exhaustion or itself garbage collected.
2018-02-06 04:51:47 +08:00
2018-03-22 12:52:20 +08:00
< a id = "s2.9.4-decision" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "294-decision" > < / a >
< a id = "generators-decision" > < / a >
#### 2.9.4 Decision
2018-02-06 04:51:47 +08:00
Fine. Use "Yields:" rather than "Returns:" in the docstring for generator
functions.
2022-05-26 03:15:47 +08:00
If the generator manages an expensive resource, make sure to force the clean up.
A good way to do the clean up is by wrapping the generator with a context
manager [PEP-0533 ](https://peps.python.org/pep-0533/ ).
2018-02-06 04:51:47 +08:00
< a id = "s2.10-lambda-functions" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "210-lambda-functions" > < / a >
< a id = "lambdas" > < / a >
### 2.10 Lambda Functions
2018-02-06 04:51:47 +08:00
2020-12-16 06:31:04 +08:00
Okay for one-liners. Prefer generator expressions over `map()` or `filter()`
with a `lambda` .
2018-02-06 04:51:47 +08:00
2018-03-22 12:52:20 +08:00
< a id = "s2.10.1-definition" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2101-definition" > < / a >
< a id = "lambdas-definition" > < / a >
#### 2.10.1 Definition
2018-02-06 04:51:47 +08:00
Lambdas define anonymous functions in an expression, as opposed to a statement.
2018-03-22 12:52:20 +08:00
< a id = "s2.10.2-pros" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2102-pros" > < / a >
< a id = "lambdas-pros" > < / a >
#### 2.10.2 Pros
2018-02-06 04:51:47 +08:00
Convenient.
2018-03-22 12:52:20 +08:00
< a id = "s2.10.3-cons" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2103-cons" > < / a >
< a id = "lambdas-cons" > < / a >
#### 2.10.3 Cons
2018-02-06 04:51:47 +08:00
Harder to read and debug than local functions. The lack of names means stack
traces are more difficult to understand. Expressiveness is limited because the
function may only contain an expression.
2018-03-22 12:52:20 +08:00
< a id = "s2.10.4-decision" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2104-decision" > < / a >
< a id = "lambdas-decision" > < / a >
#### 2.10.4 Decision
2018-02-06 04:51:47 +08:00
2018-12-06 06:40:50 +08:00
Okay to use them for one-liners. If the code inside the lambda function is
2020-08-07 03:30:10 +08:00
longer than 60-80 chars, it's probably better to define it as a regular
[nested function ](#lexical-scoping ).
2018-02-06 04:51:47 +08:00
For common operations like multiplication, use the functions from the `operator`
module instead of lambda functions. For example, prefer `operator.mul` to
`lambda x, y: x * y` .
< a id = "s2.11-conditional-expressions" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "211-conditional-expressions" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "conditional-expressions" > < / a >
2019-10-10 15:40:18 +08:00
### 2.11 Conditional Expressions
2018-02-06 04:51:47 +08:00
2019-10-10 15:40:18 +08:00
Okay for simple cases.
2018-02-06 04:51:47 +08:00
2018-03-22 12:52:20 +08:00
< a id = "s2.11.1-definition" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2111-definition" > < / a >
< a id = "conditional-expressions-definition" > < / a >
#### 2.11.1 Definition
2018-02-06 04:51:47 +08:00
Conditional expressions (sometimes called a “ternary operator”) are mechanisms
2020-08-07 03:30:10 +08:00
that provide a shorter syntax for if statements. For example: `x = 1 if cond
else 2`.
2018-02-06 04:51:47 +08:00
2018-03-22 12:52:20 +08:00
< a id = "s2.11.2-pros" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2112-pros" > < / a >
< a id = "conditional-expressions-pros" > < / a >
#### 2.11.2 Pros
2018-02-06 04:51:47 +08:00
Shorter and more convenient than an if statement.
2018-03-22 12:52:20 +08:00
< a id = "s2.11.3-cons" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2113-cons" > < / a >
< a id = "conditional-expressions-cons" > < / a >
#### 2.11.3 Cons
2018-02-06 04:51:47 +08:00
May be harder to read than an if statement. The condition may be difficult to
locate if the expression is long.
2018-03-22 12:52:20 +08:00
< a id = "s2.11.4-decision" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2114-decision" > < / a >
2018-02-06 04:51:47 +08:00
2019-10-10 15:40:18 +08:00
< a id = "conditional-expressions-decision" > < / a >
#### 2.11.4 Decision
Okay to use for simple cases. Each portion must fit on one line:
true-expression, if-expression, else-expression. Use a complete if statement
when things get more complicated.
```python
2020-12-16 06:31:04 +08:00
Yes:
one_line = 'yes' if predicate(value) else 'no'
slightly_split = ('yes' if predicate(value)
else 'no, nein, nyet')
the_longest_ternary_style_that_can_be_done = (
'yes, true, affirmative, confirmed, correct'
if predicate(value)
else 'no, false, negative, nay')
2019-10-10 15:40:18 +08:00
```
```python
2020-12-16 06:31:04 +08:00
No:
bad_line_breaking = ('yes' if predicate(value) else
'no')
portion_too_long = ('yes'
if some_long_module.some_long_predicate_function(
really_long_variable_name)
else 'no, false, negative, nay')
2019-10-10 15:40:18 +08:00
```
2018-02-06 04:51:47 +08:00
< a id = "s2.12-default-argument-values" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "212-default-argument-values" > < / a >
< a id = "default-arguments" > < / a >
### 2.12 Default Argument Values
2018-02-06 04:51:47 +08:00
Okay in most cases.
2018-03-22 12:52:20 +08:00
< a id = "s2.12.1-definition" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2121-definition" > < / a >
< a id = "default-arguments-definition" > < / a >
#### 2.12.1 Definition
2018-02-06 04:51:47 +08:00
You can specify values for variables at the end of a function's parameter list,
2020-08-07 03:30:10 +08:00
e.g., `def foo(a, b=0):` . If `foo` is called with only one argument, `b` is set
to 0. If it is called with two arguments, `b` has the value of the second
argument.
2018-02-06 04:51:47 +08:00
2018-03-22 12:52:20 +08:00
< a id = "s2.12.2-pros" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2122-pros" > < / a >
< a id = "default-arguments-pros" > < / a >
#### 2.12.2 Pros
2018-02-06 04:51:47 +08:00
2019-10-10 15:40:18 +08:00
Often you have a function that uses lots of default values, but on rare
occasions you want to override the defaults. Default argument values provide an
easy way to do this, without having to define lots of functions for the rare
exceptions. As Python does not support overloaded methods/functions, default
arguments are an easy way of "faking" the overloading behavior.
2018-02-06 04:51:47 +08:00
2018-03-22 12:52:20 +08:00
< a id = "s2.12.3-cons" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2123-cons" > < / a >
< a id = "default-arguments-cons" > < / a >
#### 2.12.3 Cons
2018-02-06 04:51:47 +08:00
Default arguments are evaluated once at module load time. This may cause
problems if the argument is a mutable object such as a list or a dictionary. If
the function modifies the object (e.g., by appending an item to a list), the
default value is modified.
2018-03-22 12:52:20 +08:00
< a id = "s2.12.4-decision" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2124-decision" > < / a >
< a id = "default-arguments-decision" > < / a >
#### 2.12.4 Decision
2018-02-06 04:51:47 +08:00
Okay to use with the following caveat:
Do not use mutable objects as default values in the function or method
definition.
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes: def foo(a, b=None):
if b is None:
b = []
Yes: def foo(a, b: Optional[Sequence] = None):
if b is None:
b = []
2018-11-15 23:47:37 +08:00
Yes: def foo(a, b: Sequence = ()): # Empty tuple OK since tuples are immutable
...
2018-02-06 04:51:47 +08:00
```
2018-11-19 10:21:51 +08:00
```python
2022-04-12 01:17:08 +08:00
from absl import flags
_FOO = flags.DEFINE_string(...)
2018-02-06 04:51:47 +08:00
No: def foo(a, b=[]):
...
No: def foo(a, b=time.time()): # The time the module was loaded???
...
2022-04-12 01:17:08 +08:00
No: def foo(a, b=_FOO.value): # sys.argv has not yet been parsed...
2018-02-06 04:51:47 +08:00
...
2019-12-31 05:45:23 +08:00
No: def foo(a, b: Mapping = {}): # Could still get passed to unchecked code
...
2018-02-06 04:51:47 +08:00
```
< a id = "s2.13-properties" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "213-properties" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "properties" > < / a >
2019-10-10 15:40:18 +08:00
### 2.13 Properties
2018-02-06 04:51:47 +08:00
2021-09-13 13:42:39 +08:00
Properties may be used to control getting or setting attributes that require
2021-12-08 02:42:12 +08:00
trivial computations or logic. Property implementations must match the general
expectations of regular attribute access: that they are cheap, straightforward,
and unsurprising.
2018-02-06 04:51:47 +08:00
2018-03-22 12:52:20 +08:00
< a id = "s2.13.1-definition" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2131-definition" > < / a >
< a id = "properties-definition" > < / a >
#### 2.13.1 Definition
2018-02-06 04:51:47 +08:00
A way to wrap method calls for getting and setting an attribute as a standard
2021-12-08 02:42:12 +08:00
attribute access.
2018-02-06 04:51:47 +08:00
2018-03-22 12:52:20 +08:00
< a id = "s2.13.2-pros" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2132-pros" > < / a >
< a id = "properties-pros" > < / a >
#### 2.13.2 Pros
2018-02-06 04:51:47 +08:00
2021-12-08 02:42:12 +08:00
* Allows for an attribute access and assignment API rather than
[getter and setter ](#getters-and-setters ) method calls.
* Can be used to make an attribute read-only.
* Allows calculations to be lazy.
* Provides a way to maintain the public interface of a class when the
internals evolve independently of class users.
2018-02-06 04:51:47 +08:00
2018-03-22 12:52:20 +08:00
< a id = "s2.13.3-cons" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2133-cons" > < / a >
< a id = "properties-cons" > < / a >
#### 2.13.3 Cons
2018-02-06 04:51:47 +08:00
2021-12-08 02:42:12 +08:00
* Can hide side-effects much like operator overloading.
* Can be confusing for subclasses.
2018-02-06 04:51:47 +08:00
2018-03-22 12:52:20 +08:00
< a id = "s2.13.4-decision" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2134-decision" > < / a >
< a id = "properties-decision" > < / a >
#### 2.13.4 Decision
2018-02-06 04:51:47 +08:00
2021-09-13 13:42:39 +08:00
Properties are allowed, but, like operator overloading, should only be used when
necessary and match the expectations of typical attribute access; follow the
[getters and setters ](#getters-and-setters ) rules otherwise.
For example, using a property to simply both get and set an internal attribute
isn't allowed: there is no computation occurring, so the property is unnecessary
2021-12-08 02:42:12 +08:00
([make the attribute public instead](#getters-and-setters)). In comparison,
using a property to control attribute access or to calculate a *trivially*
derived value is allowed: the logic is simple and unsurprising.
2021-09-13 13:42:39 +08:00
Properties should be created with the `@property`
[decorator ](#s2.17-function-and-method-decorators ). Manually implementing a
property descriptor is considered a [power feature ](#power-features ).
2018-02-06 04:51:47 +08:00
2021-12-08 02:42:12 +08:00
Inheritance with properties can be non-obvious. Do not use properties to
implement computations a subclass may ever want to override and extend.
2018-02-06 04:51:47 +08:00
< a id = "s2.14-truefalse-evaluations" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "214-truefalse-evaluations" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "truefalse-evaluations" > < / a >
2019-10-10 15:40:18 +08:00
### 2.14 True/False Evaluations
2018-02-06 04:51:47 +08:00
Use the "implicit" false if at all possible.
2018-03-22 12:52:20 +08:00
< a id = "s2.14.1-definition" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2141-definition" > < / a >
< a id = "truefalse-evaluations-definition" > < / a >
#### 2.14.1 Definition
2018-02-06 04:51:47 +08:00
Python evaluates certain values as `False` when in a boolean context. A quick
2020-08-07 03:30:10 +08:00
"rule of thumb" is that all "empty" values are considered false, so `0, None,
[], {}, ''` all evaluate as false in a boolean context.
2018-02-06 04:51:47 +08:00
2018-03-22 12:52:20 +08:00
< a id = "s2.14.2-pros" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2142-pros" > < / a >
< a id = "truefalse-evaluations-pros" > < / a >
#### 2.14.2 Pros
2018-02-06 04:51:47 +08:00
Conditions using Python booleans are easier to read and less error-prone. In
most cases, they're also faster.
2018-03-22 12:52:20 +08:00
< a id = "s2.14.3-cons" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2143-cons" > < / a >
< a id = "truefalse-evaluations-cons" > < / a >
#### 2.14.3 Cons
2018-02-06 04:51:47 +08:00
May look strange to C/C++ developers.
2018-03-22 12:52:20 +08:00
< a id = "s2.14.4-decision" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2144-decision" > < / a >
2018-02-06 04:51:47 +08:00
2019-10-10 15:40:18 +08:00
< a id = "truefalse-evaluations-decision" > < / a >
#### 2.14.4 Decision
2018-02-06 04:51:47 +08:00
2019-10-10 15:40:18 +08:00
Use the "implicit" false if possible, e.g., `if foo:` rather than `if foo !=
[]:`. There are a few caveats that you should keep in mind though:
2018-02-06 04:51:47 +08:00
2020-06-30 04:39:32 +08:00
- Always use `if foo is None:` (or `is not None` ) to check for a `None` value.
E.g., when testing whether a variable or argument that defaults to `None`
was set to some other value. The other value might be a value that's false
in a boolean context!
2018-02-06 04:51:47 +08:00
- Never compare a boolean variable to `False` using `==` . Use `if not x:`
instead. If you need to distinguish `False` from `None` then chain the
expressions, such as `if not x and x is not None:` .
- For sequences (strings, lists, tuples), use the fact that empty sequences
are false, so `if seq:` and `if not seq:` are preferable to `if len(seq):`
and `if not len(seq):` respectively.
- When handling integers, implicit false may involve more risk than benefit
(i.e., accidentally handling `None` as 0). You may compare a value which is
known to be an integer (and is not the result of `len()` ) against the
integer 0.
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes: if not users:
print('no users')
if i % 10 == 0:
self.handle_multiple_of_ten()
def f(x=None):
if x is None:
x = []
```
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
No: if len(users) == 0:
print('no users')
if not i % 10:
self.handle_multiple_of_ten()
def f(x=None):
x = x or []
```
- Note that `'0'` (i.e., `0` as string) evaluates to true.
2021-12-08 02:42:12 +08:00
- Note that Numpy arrays may raise an exception in an implicit boolean
context. Prefer the `.size` attribute when testing emptiness of a `np.array`
(e.g. `if not users.size` ).
2018-02-06 04:51:47 +08:00
< a id = "s2.16-lexical-scoping" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "216-lexical-scoping" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "lexical-scoping" > < / a >
2019-10-10 15:40:18 +08:00
### 2.16 Lexical Scoping
2018-02-06 04:51:47 +08:00
Okay to use.
2018-03-22 12:52:20 +08:00
< a id = "s2.16.1-definition" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2161-definition" > < / a >
< a id = "lexical-scoping-definition" > < / a >
#### 2.16.1 Definition
2018-02-06 04:51:47 +08:00
A nested Python function can refer to variables defined in enclosing functions,
2020-06-30 04:39:32 +08:00
but cannot assign to them. Variable bindings are resolved using lexical scoping,
that is, based on the static program text. Any assignment to a name in a block
will cause Python to treat all references to that name as a local variable, even
if the use precedes the assignment. If a global declaration occurs, the name is
treated as a global variable.
2018-02-06 04:51:47 +08:00
An example of the use of this feature is:
2018-11-19 10:21:51 +08:00
```python
2021-07-15 05:01:21 +08:00
def get_adder(summand1: float) -> Callable[[float], float]:
2018-02-06 04:51:47 +08:00
"""Returns a function that adds numbers to a given number."""
2021-07-15 05:01:21 +08:00
def adder(summand2: float) -> float:
2018-02-06 04:51:47 +08:00
return summand1 + summand2
return adder
```
2018-03-22 12:52:20 +08:00
< a id = "s2.16.2-pros" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2162-pros" > < / a >
< a id = "lexical-scoping-pros" > < / a >
#### 2.16.2 Pros
2018-02-06 04:51:47 +08:00
Often results in clearer, more elegant code. Especially comforting to
experienced Lisp and Scheme (and Haskell and ML and ...) programmers.
2018-03-22 12:52:20 +08:00
< a id = "s2.16.3-cons" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2163-cons" > < / a >
< a id = "lexical-scoping-cons" > < / a >
#### 2.16.3 Cons
2018-02-06 04:51:47 +08:00
Can lead to confusing bugs. Such as this example based on
[PEP-0227 ](http://www.google.com/url?sa=D&q=http://www.python.org/dev/peps/pep-0227/ ):
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
i = 4
2021-07-15 05:01:21 +08:00
def foo(x: Iterable[int]):
2018-02-06 04:51:47 +08:00
def bar():
print(i, end='')
# ...
# A bunch of code here
# ...
for i in x: # Ah, i *is* local to foo, so this is what bar sees
print(i, end='')
bar()
```
2020-08-07 03:30:10 +08:00
So `foo([1, 2, 3])` will print `1 2 3 3` ,
not `1 2 3 4` .
2018-02-06 04:51:47 +08:00
2018-03-22 12:52:20 +08:00
< a id = "s2.16.4-decision" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2164-decision" > < / a >
< a id = "lexical-scoping-decision" > < / a >
#### 2.16.4 Decision
2018-02-06 04:51:47 +08:00
Okay to use.
< a id = "s2.17-function-and-method-decorators" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "217-function-and-method-decorators" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "function-and-method-decorators" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "decorators" > < / a >
### 2.17 Function and Method Decorators
2018-02-06 04:51:47 +08:00
2020-09-17 23:56:05 +08:00
Use decorators judiciously when there is a clear advantage. Avoid `staticmethod`
and limit use of `classmethod` .
2018-02-06 04:51:47 +08:00
2018-03-22 12:52:20 +08:00
< a id = "s2.17.1-definition" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2171-definition" > < / a >
< a id = "decorators-definition" > < / a >
#### 2.17.1 Definition
2018-02-06 04:51:47 +08:00
2020-08-07 03:30:10 +08:00
[Decorators for Functions and Methods ](https://docs.python.org/3/glossary.html#term-decorator )
2018-02-06 04:51:47 +08:00
(a.k.a "the `@` notation"). One common decorator is `@property` , used for
converting ordinary methods into dynamically computed attributes. However, the
decorator syntax allows for user-defined decorators as well. Specifically, for
some function `my_decorator` , this:
2018-11-19 10:21:51 +08:00
```python
2020-06-30 04:39:32 +08:00
class C:
2018-02-06 04:51:47 +08:00
@my_decorator
def method(self):
# method body ...
```
is equivalent to:
2018-11-19 10:21:51 +08:00
```python
2020-06-30 04:39:32 +08:00
class C:
2018-11-15 23:47:37 +08:00
def method(self):
2018-02-06 04:51:47 +08:00
# method body ...
2018-11-15 23:47:37 +08:00
method = my_decorator(method)
2018-02-06 04:51:47 +08:00
```
2018-03-22 12:52:20 +08:00
< a id = "s2.17.2-pros" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2172-pros" > < / a >
< a id = "decorators-pros" > < / a >
#### 2.17.2 Pros
2018-02-06 04:51:47 +08:00
Elegantly specifies some transformation on a method; the transformation might
eliminate some repetitive code, enforce invariants, etc.
2018-03-22 12:52:20 +08:00
< a id = "s2.17.3-cons" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2173-cons" > < / a >
< a id = "decorators-cons" > < / a >
#### 2.17.3 Cons
2018-02-06 04:51:47 +08:00
Decorators can perform arbitrary operations on a function's arguments or return
values, resulting in surprising implicit behavior. Additionally, decorators
2021-12-08 02:42:12 +08:00
execute at object definition time. For module-level objects (classes, module
functions, ...) this happens at import time. Failures in decorator code are
pretty much impossible to recover from.
2018-02-06 04:51:47 +08:00
2018-03-22 12:52:20 +08:00
< a id = "s2.17.4-decision" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2174-decision" > < / a >
< a id = "decorators-decision" > < / a >
#### 2.17.4 Decision
2018-02-06 04:51:47 +08:00
Use decorators judiciously when there is a clear advantage. Decorators should
follow the same import and naming guidelines as functions. Decorator pydoc
should clearly state that the function is a decorator. Write unit tests for
decorators.
Avoid external dependencies in the decorator itself (e.g. don't rely on files,
sockets, database connections, etc.), since they might not be available when the
decorator runs (at import time, perhaps from `pydoc` or other tools). A
decorator that is called with valid parameters should (as much as possible) be
guaranteed to succeed in all cases.
Decorators are a special case of "top level code" - see [main ](#s3.17-main ) for
more discussion.
2020-09-17 23:56:05 +08:00
Never use `staticmethod` unless forced to in order to integrate with an API
2018-02-06 04:51:47 +08:00
defined in an existing library. Write a module level function instead.
2020-09-17 23:56:05 +08:00
Use `classmethod` only when writing a named constructor or a class-specific
2018-02-06 04:51:47 +08:00
routine that modifies necessary global state such as a process-wide cache.
< a id = "s2.18-threading" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "218-threading" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "threading" > < / a >
2019-10-10 15:40:18 +08:00
### 2.18 Threading
2018-02-06 04:51:47 +08:00
Do not rely on the atomicity of built-in types.
While Python's built-in data types such as dictionaries appear to have atomic
operations, there are corner cases where they aren't atomic (e.g. if `__hash__`
or `__eq__` are implemented as Python methods) and their atomicity should not be
relied upon. Neither should you rely on atomic variable assignment (since this
in turn depends on dictionaries).
Use the Queue module's `Queue` data type as the preferred way to communicate
data between threads. Otherwise, use the threading module and its locking
2020-06-30 04:39:32 +08:00
primitives. Prefer condition variables and `threading.Condition` instead of
using lower-level locks.
2018-02-06 04:51:47 +08:00
< a id = "s2.19-power-features" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "219-power-features" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "power-features" > < / a >
2019-10-10 15:40:18 +08:00
### 2.19 Power Features
2018-02-06 04:51:47 +08:00
Avoid these features.
2018-03-22 12:52:20 +08:00
< a id = "s2.19.1-definition" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2191-definition" > < / a >
< a id = "power-features-definition" > < / a >
#### 2.19.1 Definition
2018-02-06 04:51:47 +08:00
Python is an extremely flexible language and gives you many fancy features such
as custom metaclasses, access to bytecode, on-the-fly compilation, dynamic
2018-11-15 23:47:37 +08:00
inheritance, object reparenting, import hacks, reflection (e.g. some uses of
2021-07-15 05:01:21 +08:00
`getattr()` ), modification of system internals, `__del__` methods implementing
customized cleanup, etc.
2018-02-06 04:51:47 +08:00
2018-03-22 12:52:20 +08:00
< a id = "s2.19.2-pros" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2192-pros" > < / a >
< a id = "power-features-pros" > < / a >
#### 2.19.2 Pros
2018-02-06 04:51:47 +08:00
These are powerful language features. They can make your code more compact.
2018-03-22 12:52:20 +08:00
< a id = "s2.19.3-cons" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2193-cons" > < / a >
< a id = "power-features-cons" > < / a >
#### 2.19.3 Cons
2018-02-06 04:51:47 +08:00
It's very tempting to use these "cool" features when they're not absolutely
necessary. It's harder to read, understand, and debug code that's using unusual
features underneath. It doesn't seem that way at first (to the original author),
but when revisiting the code, it tends to be more difficult than code that is
longer but is straightforward.
2018-03-22 12:52:20 +08:00
< a id = "s2.19.4-decision" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2194-decision" > < / a >
< a id = "power-features-decision" > < / a >
#### 2.19.4 Decision
2018-02-06 04:51:47 +08:00
Avoid these features in your code.
Standard library modules and classes that internally use these features are okay
2020-12-16 06:31:04 +08:00
to use (for example, `abc.ABCMeta` , `dataclasses` , and `enum` ).
2018-02-06 04:51:47 +08:00
< a id = "s2.20-modern-python" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "220-modern-python" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "modern-python" > < / a >
2021-07-21 02:02:30 +08:00
### 2.20 Modern Python: from \_\_future\_\_ imports
2018-02-06 04:51:47 +08:00
2021-07-21 02:02:30 +08:00
New language version semantic changes may be gated behind a special future
import to enable them on a per-file basis within earlier runtimes.
2018-02-06 04:51:47 +08:00
2018-03-22 12:52:20 +08:00
< a id = "s2.20.1-definition" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2201-definition" > < / a >
< a id = "modern-python-definition" > < / a >
#### 2.20.1 Definition
2018-02-06 04:51:47 +08:00
2021-07-21 02:02:30 +08:00
Being able to turn on some of the more modern features via `from __future__
import` statements allows early use of features from expected future Python
versions.
2018-02-06 04:51:47 +08:00
2018-03-22 12:52:20 +08:00
< a id = "s2.20.2-pros" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2202-pros" > < / a >
< a id = "modern-python-pros" > < / a >
#### 2.20.2 Pros
2018-02-06 04:51:47 +08:00
2021-07-21 02:02:30 +08:00
This has proven to make runtime version upgrades smoother as changes can be made
on a per-file basis while declaring compatibility and preventing regressions
within those files. Modern code is more maintainable as it is less likely to
accumulate technical debt that will be problematic during future runtime
upgrades.
2018-02-06 04:51:47 +08:00
2018-03-22 12:52:20 +08:00
< a id = "s2.20.3-cons" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2203-cons" > < / a >
< a id = "modern-python-cons" > < / a >
#### 2.20.3 Cons
2018-02-06 04:51:47 +08:00
2021-07-21 02:02:30 +08:00
Such code may not work on very old interpreter versions prior to the
introduction of the needed future statement. The need for this is more common in
projects supporting an extremely wide variety of environments.
2018-02-06 04:51:47 +08:00
2018-03-22 12:52:20 +08:00
< a id = "s2.20.4-decision" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2204-decision" > < / a >
< a id = "modern-python-decision" > < / a >
#### 2.20.4 Decision
2018-02-06 04:51:47 +08:00
##### from \_\_future\_\_ imports
2021-07-21 02:02:30 +08:00
Use of `from __future__ import` statements is encouraged. It allows a given
source file to start using more modern Python syntax features today. Once you no
longer need to run on a version where the features are hidden behind a
`__future__` import, feel free to remove those lines.
In code that may execute on versions as old as 3.5 rather than >= 3.7, import:
```python
from __future__ import generator_stop
```
For more information read the
[Python future statement definitions ](https://docs.python.org/3/library/__future__.html )
documentation.
2019-10-10 15:40:18 +08:00
2021-07-21 02:02:30 +08:00
Please don't remove these imports until you are confident the code is only ever
used in a sufficiently modern environment. Even if you do not currently use the
feature a specific future import enables in your code today, keeping it in place
in the file prevents later modifications of the code from inadvertently
depending on the older behavior.
2018-11-15 23:47:37 +08:00
2022-05-26 03:15:47 +08:00
Use other `from __future__` import statements as you see fit.
2018-02-06 04:51:47 +08:00
2020-06-30 04:39:32 +08:00
< a id = "s2.21-type-annotated-code" > < / a >
< a id = "s2.21-typed-code" > < / a >
< a id = "221-type-annotated-code" > < / a >
< a id = "typed-code" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "typed-code" > < / a >
### 2.21 Type Annotated Code
2018-02-06 04:51:47 +08:00
2022-05-26 03:15:47 +08:00
You can annotate Python code with type hints according to
2018-02-06 04:51:47 +08:00
[PEP-484 ](https://www.python.org/dev/peps/pep-0484/ ), and type-check the code at
2020-08-07 03:30:10 +08:00
build time with a type checking tool like [pytype ](https://github.com/google/pytype ).
2018-02-06 04:51:47 +08:00
2020-08-07 03:30:10 +08:00
Type annotations can be in the source or in a
[stub pyi file ](https://www.python.org/dev/peps/pep-0484/#stub-files ). Whenever
possible, annotations should be in the source. Use pyi files for third-party or
extension modules.
2018-02-06 04:51:47 +08:00
2018-03-22 12:52:20 +08:00
< a id = "s2.21.1-definition" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2211-definition" > < / a >
< a id = "typed-code-definition" > < / a >
#### 2.21.1 Definition
2018-02-06 04:51:47 +08:00
Type annotations (or "type hints") are for function or method arguments and
return values:
2018-11-19 10:21:51 +08:00
```python
2022-04-12 01:17:08 +08:00
def func(a: int) -> list[int]:
2018-02-06 04:51:47 +08:00
```
2020-06-30 04:39:32 +08:00
You can also declare the type of a variable using similar
[PEP-526 ](https://www.python.org/dev/peps/pep-0526/ ) syntax:
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2020-06-30 04:39:32 +08:00
a: SomeType = some_func()
```
2018-03-22 12:52:20 +08:00
< a id = "s2.21.2-pros" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2212-pros" > < / a >
< a id = "typed-code-pros" > < / a >
#### 2.21.2 Pros
2018-02-06 04:51:47 +08:00
Type annotations improve the readability and maintainability of your code. The
type checker will convert many runtime errors to build-time errors, and reduce
your ability to use [Power Features ](#power-features ).
2018-03-22 12:52:20 +08:00
< a id = "s2.21.3-cons" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2213-cons" > < / a >
< a id = "typed-code-cons" > < / a >
#### 2.21.3 Cons
2018-02-06 04:51:47 +08:00
2020-08-07 03:30:10 +08:00
You will have to keep the type declarations up to date.
You might see type errors that you think are
valid code. Use of a
[type checker ](https://github.com/google/pytype )
2018-02-06 04:51:47 +08:00
may reduce your ability to use [Power Features ](#power-features ).
2018-03-22 12:52:20 +08:00
< a id = "s2.21.4-decision" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "2214-decision" > < / a >
2018-02-06 04:51:47 +08:00
2019-10-10 15:40:18 +08:00
< a id = "typed-code-decision" > < / a >
#### 2.21.4 Decision
2018-02-06 04:51:47 +08:00
2019-10-10 15:40:18 +08:00
You are strongly encouraged to enable Python type analysis when updating code.
When adding or modifying public APIs, include type annotations and enable
checking via pytype in the build system. As static analysis is relatively new to
Python, we acknowledge that undesired side-effects (such as
wrongly
inferred types) may prevent adoption by some projects. In those situations,
authors are encouraged to add a comment with a TODO or link to a bug describing
the issue(s) currently preventing type annotation adoption in the BUILD file or
in the code itself as appropriate.
2018-02-06 04:51:47 +08:00
< a id = "s3-python-style-rules" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "3-python-style-rules" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "python-style-rules" > < / a >
2019-10-10 15:40:18 +08:00
## 3 Python Style Rules
2018-02-06 04:51:47 +08:00
< a id = "s3.1-semicolons" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "31-semicolons" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "semicolons" > < / a >
2019-10-10 15:40:18 +08:00
### 3.1 Semicolons
2018-02-06 04:51:47 +08:00
2018-12-06 06:40:50 +08:00
Do not terminate your lines with semicolons, and do not use semicolons to put
2018-02-06 04:51:47 +08:00
two statements on the same line.
< a id = "s3.2-line-length" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "32-line-length" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "line-length" > < / a >
2019-10-10 15:40:18 +08:00
### 3.2 Line length
2018-02-06 04:51:47 +08:00
Maximum line length is *80 characters* .
2019-10-10 15:40:18 +08:00
Explicit exceptions to the 80 character limit:
2018-02-06 04:51:47 +08:00
- Long import statements.
- URLs, pathnames, or long flags in comments.
- Long string module level constants not containing whitespace that would be
inconvenient to split across lines such as URLs or pathnames.
2020-08-07 03:30:10 +08:00
- Pylint disable comments. (e.g.: `# pylint: disable=invalid-name` )
2018-02-06 04:51:47 +08:00
Do not use backslash line continuation except for `with` statements requiring
three or more context managers.
2020-08-07 03:30:10 +08:00
Make use of Python's
[implicit line joining inside parentheses, brackets and braces ](http://docs.python.org/reference/lexical_analysis.html#implicit-line-joining ).
2018-02-06 04:51:47 +08:00
If necessary, you can add an extra pair of parentheses around an expression.
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes: foo_bar(self, width, height, color='black', design=None, x='foo',
emphasis=None, highlight=0)
if (width == 0 and height == 0 and
color == 'red' and emphasis == 'strong'):
```
When a literal string won't fit on a single line, use parentheses for implicit
line joining.
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
x = ('This will build a very long long '
'long long long long long long string')
```
Within comments, put long URLs on their own line if necessary.
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes: # See details at
# http://www.example.com/us/developer/documentation/api/content/v2.0/csv_file_name_extension_full_specification.html
```
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
No: # See details at
# http://www.example.com/us/developer/documentation/api/content/\
# v2.0/csv_file_name_extension_full_specification.html
```
It is permissible to use backslash continuation when defining a `with` statement
whose expressions span three or more lines. For two lines of expressions, use a
nested `with` statement:
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes: with very_long_first_expression_function() as spam, \
very_long_second_expression_function() as beans, \
third_thing() as eggs:
place_order(eggs, beans, spam, beans)
```
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
No: with VeryLongFirstExpressionFunction() as spam, \
VeryLongSecondExpressionFunction() as beans:
2021-05-07 06:53:45 +08:00
PlaceOrder(beans, spam)
2018-02-06 04:51:47 +08:00
```
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes: with very_long_first_expression_function() as spam:
with very_long_second_expression_function() as beans:
place_order(beans, spam)
```
Make note of the indentation of the elements in the line continuation examples
above; see the [indentation ](#s3.4-indentation ) section for explanation.
2019-10-10 15:40:18 +08:00
In all other cases where a line exceeds 80 characters, and the
[yapf ](https://github.com/google/yapf/ )
auto-formatter does not help bring the line below the limit, the line is allowed
2021-05-10 10:32:30 +08:00
to exceed this maximum. Authors are encouraged to manually break the line up per
the notes above when it is sensible.
2019-10-10 15:40:18 +08:00
2018-02-06 04:51:47 +08:00
< a id = "s3.3-parentheses" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "33-parentheses" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "parentheses" > < / a >
2019-10-10 15:40:18 +08:00
### 3.3 Parentheses
2018-02-06 04:51:47 +08:00
Use parentheses sparingly.
It is fine, though not required, to use parentheses around tuples. Do not use
them in return statements or conditional statements unless using parentheses for
implied line continuation or to indicate a tuple.
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes: if foo:
bar()
while x:
x = bar()
if x and y:
bar()
if not x:
bar()
# For a 1 item tuple the ()s are more visually obvious than the comma.
onesie = (foo,)
return foo
return spam, beans
return (spam, beans)
for (x, y) in dict.items(): ...
```
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
No: if (x):
bar()
if not(x):
bar()
return (foo)
```
< a id = "s3.4-indentation" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "34-indentation" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "indentation" > < / a >
2019-10-10 15:40:18 +08:00
### 3.4 Indentation
2018-02-06 04:51:47 +08:00
Indent your code blocks with *4 spaces* .
Never use tabs or mix tabs and spaces. In cases of implied line continuation,
you should align wrapped elements either vertically, as per the examples in the
[line length ](#s3.2-line-length ) section; or using a hanging indent of 4 spaces,
in which case there should be nothing after the open parenthesis or bracket on
the first line.
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes: # Aligned with opening delimiter
foo = long_function_name(var_one, var_two,
var_three, var_four)
meal = (spam,
beans)
# Aligned with opening delimiter in a dictionary
foo = {
2021-07-15 05:01:21 +08:00
'long_dictionary_key': value1 +
value2,
2018-02-06 04:51:47 +08:00
...
}
# 4-space hanging indent; nothing on first line
foo = long_function_name(
var_one, var_two, var_three,
var_four)
meal = (
spam,
beans)
# 4-space hanging indent in a dictionary
foo = {
2021-07-15 05:01:21 +08:00
'long_dictionary_key':
2018-02-06 04:51:47 +08:00
long_dictionary_value,
...
}
```
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
No: # Stuff on first line forbidden
foo = long_function_name(var_one, var_two,
var_three, var_four)
meal = (spam,
beans)
# 2-space hanging indent forbidden
foo = long_function_name(
var_one, var_two, var_three,
var_four)
# No hanging indent in a dictionary
foo = {
2021-07-15 05:01:21 +08:00
'long_dictionary_key':
2018-02-06 04:51:47 +08:00
long_dictionary_value,
...
}
```
2020-06-30 04:39:32 +08:00
< a id = "s3.4.1-trailing-comma" > < / a >
< a id = "s3.4.1-trailing-commas" > < / a >
2018-11-15 23:47:37 +08:00
< a id = "s3.4.1-trailing_comma" > < / a >
2020-06-30 04:39:32 +08:00
< a id = "s3.4.1-trailing_commas" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "341-trailing_comma" > < / a >
2020-06-30 04:39:32 +08:00
< a id = "341-trailing_commas" > < / a >
2018-11-15 23:47:37 +08:00
< a id = "trailing_comma" > < / a >
2020-06-30 04:39:32 +08:00
< a id = "trailing_commas" > < / a >
2018-11-15 23:47:37 +08:00
2019-10-10 15:40:18 +08:00
< a id = "trailing-comma" > < / a >
2021-07-15 05:01:21 +08:00
#### 3.4.1 Trailing commas in sequences of items?
2018-11-15 23:47:37 +08:00
Trailing commas in sequences of items are recommended only when the closing
container token `]` , `)` , or `}` does not appear on the same line as the final
element. The presence of a trailing comma is also used as a hint to our Python
code auto-formatter [YAPF ](https://pypi.org/project/yapf/ ) to direct it to auto-format the container
of items to one item per line when the `,` after the final element is present.
2018-11-19 10:21:51 +08:00
```python
2018-11-15 23:47:37 +08:00
Yes: golomb3 = [0, 1, 3]
Yes: golomb4 = [
0,
1,
4,
6,
]
```
2018-11-19 10:21:51 +08:00
```python
2018-11-15 23:47:37 +08:00
No: golomb4 = [
0,
1,
4,
6
]
```
2018-02-06 04:51:47 +08:00
< a id = "s3.5-blank-lines" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "35-blank-lines" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "blank-lines" > < / a >
2019-10-10 15:40:18 +08:00
### 3.5 Blank Lines
2018-02-06 04:51:47 +08:00
Two blank lines between top-level definitions, be they function or class
definitions. One blank line between method definitions and between the `class`
2018-11-29 02:15:08 +08:00
line and the first method. No blank line following a `def` line. Use single
blank lines as you judge appropriate within functions or methods.
2018-02-06 04:51:47 +08:00
2021-09-13 13:42:39 +08:00
Blank lines need not be anchored to the definition. For example, related
comments immediately preceding function, class, and method definitions can make
sense. Consider if your comment might be more useful as part of the docstring.
2018-02-06 04:51:47 +08:00
< a id = "s3.6-whitespace" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "36-whitespace" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "whitespace" > < / a >
2019-10-10 15:40:18 +08:00
### 3.6 Whitespace
2018-02-06 04:51:47 +08:00
Follow standard typographic rules for the use of spaces around punctuation.
No whitespace inside parentheses, brackets or braces.
2018-11-19 10:21:51 +08:00
```python
2021-07-15 05:01:21 +08:00
Yes: spam(ham[1], {'eggs': 2}, [])
2018-02-06 04:51:47 +08:00
```
2018-11-19 10:21:51 +08:00
```python
2021-07-15 05:01:21 +08:00
No: spam( ham[ 1 ], { 'eggs': 2 }, [ ] )
2018-02-06 04:51:47 +08:00
```
No whitespace before a comma, semicolon, or colon. Do use whitespace after a
2018-12-06 06:40:50 +08:00
comma, semicolon, or colon, except at the end of the line.
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes: if x == 4:
print(x, y)
x, y = y, x
```
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
No: if x == 4 :
print(x , y)
x , y = y , x
```
No whitespace before the open paren/bracket that starts an argument list,
indexing or slicing.
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes: spam(1)
```
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
No: spam (1)
```
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes: dict['key'] = list[index]
```
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
No: dict ['key'] = list [index]
```
2019-10-10 15:40:18 +08:00
No trailing whitespace.
2018-02-06 04:51:47 +08:00
Surround binary operators with a single space on either side for assignment
(`=`), comparisons (`==, < , >, !=, < >, < =, >=, in, not in, is, is not`), and
Booleans (`and, or, not`). Use your better judgment for the insertion of spaces
2018-11-15 23:47:37 +08:00
around arithmetic operators (`+`, `-` , `*` , `/` , `//` , `%` , `**` , `@` ).
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes: x == 1
```
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
No: x< 1
```
2018-12-06 06:40:50 +08:00
Never use spaces around `=` when passing keyword arguments or defining a default
2020-08-07 03:30:10 +08:00
parameter value, with one exception:
2021-12-08 02:42:12 +08:00
[when a type annotation is present ](#typing-default-values ), *do* use spaces
2020-08-07 03:30:10 +08:00
around the `=` for the default parameter value.
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes: def complex(real, imag=0.0): return Magic(r=real, i=imag)
Yes: def complex(real, imag: float = 0.0): return Magic(r=real, i=imag)
```
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
No: def complex(real, imag = 0.0): return Magic(r = real, i = imag)
No: def complex(real, imag: float=0.0): return Magic(r = real, i = imag)
```
Don't use spaces to vertically align tokens on consecutive lines, since it
becomes a maintenance burden (applies to `:` , `#` , `=` , etc.):
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes:
foo = 1000 # comment
long_name = 2 # comment that should not be aligned
dictionary = {
'foo': 1,
'long_name': 2,
}
```
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
No:
foo = 1000 # comment
long_name = 2 # comment that should not be aligned
dictionary = {
'foo' : 1,
'long_name': 2,
}
```
< a id = "Python_Interpreter" > < / a >
< a id = "s3.7-shebang-line" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "37-shebang-line" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "shebang-line" > < / a >
2019-10-10 15:40:18 +08:00
### 3.7 Shebang Line
2018-02-06 04:51:47 +08:00
Most `.py` files do not need to start with a `#!` line. Start the main file of a
program with
2020-12-16 06:31:04 +08:00
`#!/usr/bin/env python3` (to support virtualenvs) or `#!/usr/bin/python3` per
[PEP-394 ](https://www.python.org/dev/peps/pep-0394/ ).
2018-02-06 04:51:47 +08:00
2020-12-16 06:31:04 +08:00
This line is used by the kernel to find the Python interpreter, but is ignored by Python when importing modules. It is only necessary on a file intended to be executed directly.
2018-02-06 04:51:47 +08:00
2020-06-30 04:39:32 +08:00
< a id = "s3.8-comments-and-docstrings" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "s3.8-comments" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "38-comments-and-docstrings" > < / a >
< a id = "documentation" > < / a >
### 3.8 Comments and Docstrings
2018-02-06 04:51:47 +08:00
Be sure to use the right style for module, function, method docstrings and
2018-11-15 23:47:37 +08:00
inline comments.
2018-02-06 04:51:47 +08:00
< a id = "s3.8.1-comments-in-doc-strings" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "381-docstrings" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "comments-in-doc-strings" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "docstrings" > < / a >
#### 3.8.1 Docstrings
2018-02-06 04:51:47 +08:00
2021-12-08 02:42:12 +08:00
Python uses *docstrings* to document code. A docstring is a string that is the
2018-02-06 04:51:47 +08:00
first statement in a package, module, class or function. These strings can be
extracted automatically through the `__doc__` member of the object and are used
by `pydoc` .
(Try running `pydoc` on your module to see how it looks.) Always use the three
2020-08-07 03:30:10 +08:00
double-quote `"""` format for docstrings (per
[PEP 257 ](https://www.google.com/url?sa=D&q=http://www.python.org/dev/peps/pep-0257/ )).
A docstring should be organized as a summary line (one physical line not
exceeding 80 characters) terminated by a period, question mark, or exclamation
point. When writing more (encouraged), this must be followed by a blank line,
2018-02-06 04:51:47 +08:00
followed by the rest of the docstring starting at the same cursor position as
the first quote of the first line. There are more formatting guidelines for
docstrings below.
< a id = "s3.8.2-comments-in-modules" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "382-modules" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "comments-in-modules" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "module-docs" > < / a >
#### 3.8.2 Modules
2020-08-07 03:30:10 +08:00
Every file should contain license boilerplate. Choose the appropriate boilerplate for the license used by the project (for example, Apache 2.0, BSD, LGPL, GPL)
2019-10-10 15:40:18 +08:00
Files should start with a docstring describing the contents and usage of the
module.
```python
"""A one line summary of the module or program, terminated by a period.
Leave one blank line. The rest of this docstring should contain an
overall description of the module or program. Optionally, it may also
contain a brief description of exported classes and functions and/or usage
examples.
Typical usage example:
foo = ClassFoo()
bar = foo.FunctionBar()
"""
```
2018-02-06 04:51:47 +08:00
< a id = "s3.8.3-functions-and-methods" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "383-functions-and-methods" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "functions-and-methods" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "function-docs" > < / a >
#### 3.8.3 Functions and Methods
2018-02-06 04:51:47 +08:00
2018-12-06 06:40:50 +08:00
In this section, "function" means a method, function, or generator.
2018-02-06 04:51:47 +08:00
A function must have a docstring, unless it meets all of the following criteria:
- not externally visible
- very short
- obvious
A docstring should give enough information to write a call to the function
2021-07-15 05:01:21 +08:00
without reading the function's code. The docstring should describe the
function's calling syntax and its semantics, but generally not its
implementation details, unless those details are relevant to how the function is
to be used. For example, a function that mutates one of its arguments as a side
effect should note that in its docstring. Otherwise, subtle but important
details of a function's implementation that are not relevant to the caller are
better expressed as comments alongside the code than within the function's
docstring.
The docstring should be descriptive-style (`"""Fetches rows from a
Bigtable."""`) rather than imperative-style (`"""Fetch rows from a
Bigtable."""`). The docstring for a `@property` data descriptor should use the
same style as the docstring for an attribute or a
< a href = "#doc-function-args" > function argument< / a > (`"""The Bigtable path."""`,
rather than `"""Returns the Bigtable path."""` ).
2018-02-06 04:51:47 +08:00
A method that overrides a method from a base class may have a simple docstring
2018-12-06 06:40:50 +08:00
sending the reader to its overridden method's docstring, such as `"""See base
class."""`. The rationale is that there is no need to repeat in many places
documentation that is already present in the base method's docstring. However,
if the overriding method's behavior is substantially different from the
overridden method, or details need to be provided (e.g., documenting additional
side effects), a docstring with at least those differences is required on the
overriding method.
2018-02-06 04:51:47 +08:00
Certain aspects of a function should be documented in special sections, listed
2019-10-10 15:40:18 +08:00
below. Each section begins with a heading line, which ends with a colon. All
sections other than the heading should maintain a hanging indent of two or four
spaces (be consistent within a file). These sections can be omitted in cases
where the function's name and signature are informative enough that it can be
aptly described using a one-line docstring.
2018-02-06 04:51:47 +08:00
2018-11-29 02:15:08 +08:00
< a id = "doc-function-args" > < / a >
[*Args:* ](#doc-function-args )
: List each parameter by name. A description should follow the name, and be
2020-07-09 04:01:02 +08:00
separated by a colon followed by either a space or newline. If the
description is too long to fit on a single 80-character line, use a hanging
indent of 2 or 4 spaces more than the parameter name (be consistent with the
2020-08-07 03:30:10 +08:00
rest of the docstrings in the file). The description should include required
type(s) if the code does not contain a corresponding type annotation. If a
function accepts `*foo` (variable length argument lists) and/or `**bar`
(arbitrary keyword arguments), they should be listed as `*foo` and `**bar` .
2018-11-29 02:15:08 +08:00
< a id = "doc-function-returns" > < / a >
[*Returns:* (or *Yields:* for generators) ](#doc-function-returns )
2018-11-15 23:47:37 +08:00
: Describe the type and semantics of the return value. If the function only
2019-10-10 15:40:18 +08:00
returns None, this section is not required. It may also be omitted if the
docstring starts with Returns or Yields (e.g. `"""Returns row from Bigtable
as a tuple of strings."""`) and the opening sentence is sufficient to
2021-09-13 13:42:39 +08:00
describe the return value. Do not imitate 'NumPy style'
([example](http://numpy.org/doc/stable/reference/generated/numpy.linalg.qr.html)),
which frequently documents a tuple return value as if it were multiple
return values with individual names (never mentioning the tuple). Instead,
2022-04-12 01:17:08 +08:00
describe such a return value as: "Returns: A tuple (mat_a, mat_b), where
2021-09-13 13:42:39 +08:00
mat_a is ..., and ...". The auxiliary names in the docstring need not
necessarily correspond to any internal names used in the function body (as
those are not part of the API).
2018-02-06 04:51:47 +08:00
2018-11-29 02:15:08 +08:00
< a id = "doc-function-raises" > < / a >
[*Raises:* ](#doc-function-raises )
2020-07-09 04:01:02 +08:00
: List all exceptions that are relevant to the interface followed by a
description. Use a similar exception name + colon + space or newline and
hanging indent style as described in *Args:* . You should not document
exceptions that get raised if the API specified in the docstring is violated
(because this would paradoxically make behavior under violation of the API
part of the API).
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2020-07-09 04:01:02 +08:00
def fetch_smalltable_rows(table_handle: smalltable.Table,
keys: Sequence[Union[bytes, str]],
require_all_keys: bool = False,
2022-04-12 01:17:08 +08:00
) -> Mapping[bytes, tuple[str, ...]]:
2020-07-09 04:01:02 +08:00
"""Fetches rows from a Smalltable.
2018-02-06 04:51:47 +08:00
Retrieves rows pertaining to the given keys from the Table instance
2020-07-09 04:01:02 +08:00
represented by table_handle. String keys will be UTF-8 encoded.
2018-02-06 04:51:47 +08:00
Args:
2020-07-09 04:01:02 +08:00
table_handle: An open smalltable.Table instance.
keys: A sequence of strings representing the key of each table
row to fetch. String keys will be UTF-8 encoded.
2021-09-13 13:42:39 +08:00
require_all_keys: If True only rows with values set for all keys will be
returned.
2018-02-06 04:51:47 +08:00
Returns:
A dict mapping keys to the corresponding table row data
fetched. Each row is represented as a tuple of strings. For
example:
2020-07-09 04:01:02 +08:00
{b'Serak': ('Rigel VII', 'Preparer'),
b'Zim': ('Irk', 'Invader'),
b'Lrrr': ('Omicron Persei 8', 'Emperor')}
Returned keys are always bytes. If a key from the keys argument is
missing from the dictionary, then that row was not found in the
table (and require_all_keys must have been False).
Raises:
IOError: An error occurred accessing the smalltable.
"""
```
Similarly, this variation on `Args:` with a line break is also allowed:
```python
def fetch_smalltable_rows(table_handle: smalltable.Table,
keys: Sequence[Union[bytes, str]],
require_all_keys: bool = False,
2022-04-12 01:17:08 +08:00
) -> Mapping[bytes, tuple[str, ...]]:
2020-07-09 04:01:02 +08:00
"""Fetches rows from a Smalltable.
Retrieves rows pertaining to the given keys from the Table instance
represented by table_handle. String keys will be UTF-8 encoded.
Args:
table_handle:
An open smalltable.Table instance.
keys:
A sequence of strings representing the key of each table row to
fetch. String keys will be UTF-8 encoded.
require_all_keys:
2021-09-13 13:42:39 +08:00
If True only rows with values set for all keys will be returned.
2020-07-09 04:01:02 +08:00
Returns:
A dict mapping keys to the corresponding table row data
fetched. Each row is represented as a tuple of strings. For
example:
{b'Serak': ('Rigel VII', 'Preparer'),
b'Zim': ('Irk', 'Invader'),
b'Lrrr': ('Omicron Persei 8', 'Emperor')}
2018-02-06 04:51:47 +08:00
2020-07-09 04:01:02 +08:00
Returned keys are always bytes. If a key from the keys argument is
missing from the dictionary, then that row was not found in the
table (and require_all_keys must have been False).
2018-02-06 04:51:47 +08:00
Raises:
2020-07-09 04:01:02 +08:00
IOError: An error occurred accessing the smalltable.
2018-02-06 04:51:47 +08:00
"""
```
< a id = "s3.8.4-comments-in-classes" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "384-classes" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "comments-in-classes" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "class-docs" > < / a >
#### 3.8.4 Classes
2018-02-06 04:51:47 +08:00
Classes should have a docstring below the class definition describing the class.
If your class has public attributes, they should be documented here in an
2018-11-15 23:47:37 +08:00
`Attributes` section and follow the same formatting as a
[function's `Args` ](#doc-function-args ) section.
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2020-06-30 04:39:32 +08:00
class SampleClass:
2018-02-06 04:51:47 +08:00
"""Summary of class here.
2022-05-26 03:15:47 +08:00
Longer class information...
Longer class information...
2018-02-06 04:51:47 +08:00
Attributes:
likes_spam: A boolean indicating if we like SPAM or not.
eggs: An integer count of the eggs we have laid.
"""
2021-07-15 05:01:21 +08:00
def __init__ (self, likes_spam: bool = False):
2018-02-06 04:51:47 +08:00
"""Inits SampleClass with blah."""
self.likes_spam = likes_spam
self.eggs = 0
def public_method(self):
"""Performs operation blah."""
```
2022-05-26 03:15:47 +08:00
All class docstrings should start with a one-line summary that describes what
the class instance represents. This implies that subclasses of `Exception`
should also describe what the exception represents, and not the context in which
it might occur. The class docstring should not repeat unnecessary information,
such as that the class is a class.
```python
class CheeseShopAddress:
"""The address of a cheese shop.
...
"""
class OutOfCheeseError(Exception):
"""No more cheese is available."""
```
```python
class CheeseShopAddress:
"""Class that describes the address of a cheese shop.
...
"""
class OutOfCheeseError(Exception):
"""Raised when no more cheese is available."""
```
2020-06-30 04:39:32 +08:00
< a id = "s3.8.5-block-and-inline-comments" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "comments-in-block-and-inline" > < / a >
< a id = "s3.8.5-comments-in-block-and-inline" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "385-block-and-inline-comments" > < / a >
< a id = "comments" > < / a >
#### 3.8.5 Block and Inline Comments
2018-02-06 04:51:47 +08:00
The final place to have comments is in tricky parts of the code. If you're going
2020-08-07 03:30:10 +08:00
to have to explain it at the next [code review ](http://en.wikipedia.org/wiki/Code_review ),
you should comment it now. Complicated operations get a few lines of comments
before the operations commence. Non-obvious ones get comments at the end of the
line.
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
# We use a weighted dictionary search to find out where i is in
# the array. We extrapolate position based on the largest num
# in the array and the array size and then do binary search to
# get the exact number.
if i & (i-1) == 0: # True if i is 0 or a power of 2.
```
2019-10-10 15:40:18 +08:00
To improve legibility, these comments should start at least 2 spaces away from
the code with the comment character `#` , followed by at least one space before
the text of the comment itself.
2018-02-06 04:51:47 +08:00
On the other hand, never describe the code. Assume the person reading the code
knows Python (though not what you're trying to do) better than you do.
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
# BAD COMMENT: Now go through the b array and make sure whenever i occurs
# the next element is i+1
```
<!-- The next section is copied from the C++ style guide. -->
2019-10-10 15:40:18 +08:00
2018-02-06 04:51:47 +08:00
< a id = "s3.8.6-punctuation-spelling-and-grammar" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "386-punctuation-spelling-and-grammar" > < / a >
< a id = "spelling" > < / a >
< a id = "punctuation" > < / a >
< a id = "grammar" > < / a >
< a id = "punctuation-spelling-grammar" > < / a >
2019-12-31 05:45:23 +08:00
#### 3.8.6 Punctuation, Spelling, and Grammar
2018-02-06 04:51:47 +08:00
Pay attention to punctuation, spelling, and grammar; it is easier to read
well-written comments than badly written ones.
Comments should be as readable as narrative text, with proper capitalization and
punctuation. In many cases, complete sentences are more readable than sentence
fragments. Shorter comments, such as comments at the end of a line of code, can
sometimes be less formal, but you should be consistent with your style.
Although it can be frustrating to have a code reviewer point out that you are
using a comma when you should be using a semicolon, it is very important that
source code maintain a high level of clarity and readability. Proper
punctuation, spelling, and grammar help with that goal.
< a id = "s3.10-strings" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "310-strings" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "strings" > < / a >
2019-10-10 15:40:18 +08:00
### 3.10 Strings
2018-02-06 04:51:47 +08:00
2020-12-16 06:31:04 +08:00
Use an
[f-string ](https://docs.python.org/3/reference/lexical_analysis.html#f-strings ),
the `%` operator, or the `format` method for formatting strings, even when the
2022-04-12 01:17:08 +08:00
parameters are all strings. Use your best judgment to decide between `+` and
string formatting.
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2022-04-12 01:17:08 +08:00
Yes: x = f'name: {name}; score: {n}'
2018-02-06 04:51:47 +08:00
x = '%s, %s!' % (imperative, expletive)
x = '{}, {}'.format(first, second)
x = 'name: %s; score: %d' % (name, n)
x = 'name: {}; score: {}'.format(name, n)
2022-04-12 01:17:08 +08:00
x = a + b
2018-02-06 04:51:47 +08:00
```
2018-11-19 10:21:51 +08:00
```python
2022-04-12 01:17:08 +08:00
No: x = first + ', ' + second
2018-02-06 04:51:47 +08:00
x = 'name: ' + name + '; score: ' + str(n)
```
2020-12-16 06:31:04 +08:00
Avoid using the `+` and `+=` operators to accumulate a string within a loop. In
some conditions, accumulating a string with addition can lead to quadratic
rather than linear running time. Although common accumulations of this sort may
be optimized on CPython, that is an implementation detail. The conditions under
which an optimization applies are not easy to predict and may change. Instead,
add each substring to a list and `''.join` the list after the loop terminates,
or write each substring to an `io.StringIO` buffer. These techniques
consistently have amortized-linear run time complexity.
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes: items = ['< table > ']
for last_name, first_name in employee_list:
items.append('< tr > < td > %s, %s< / td > < / tr > ' % (last_name, first_name))
items.append('< / table > ')
employee_table = ''.join(items)
```
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
No: employee_table = '< table > '
for last_name, first_name in employee_list:
employee_table += '< tr > < td > %s, %s< / td > < / tr > ' % (last_name, first_name)
employee_table += '< / table > '
```
Be consistent with your choice of string quote character within a file. Pick `'`
or `"` and stick with it. It is okay to use the other quote character on a
2021-09-13 13:42:39 +08:00
string to avoid the need to backslash-escape quote characters within the string.
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes:
Python('Why are you hiding your eyes?')
Gollum("I'm scared of lint errors.")
Narrator('"Good!" thought a happy Python reviewer.')
```
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
No:
Python("Why are you hiding your eyes?")
Gollum('The lint. It burns. It burns us.')
Gollum("Always the great lint. Watching. Watching.")
```
Prefer `"""` for multi-line strings rather than `'''` . Projects may choose to
use `'''` for all non-docstring multi-line strings if and only if they also use
2019-10-10 15:40:18 +08:00
`'` for regular strings. Docstrings must use `"""` regardless.
Multi-line strings do not flow with the indentation of the rest of the program.
If you need to avoid embedding extra space in the string, use either
concatenated single-line strings or a multi-line string with
[`textwrap.dedent()` ](https://docs.python.org/3/library/textwrap.html#textwrap.dedent )
to remove the initial space on each line:
```python
No:
long_string = """This is pretty ugly.
Don't do this.
"""
```
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes:
2019-10-10 15:40:18 +08:00
long_string = """This is fine if your use case can accept
extraneous leading spaces."""
2018-02-06 04:51:47 +08:00
```
2018-11-19 10:21:51 +08:00
```python
2019-10-10 15:40:18 +08:00
Yes:
2020-06-30 04:39:32 +08:00
long_string = ("And this is fine if you cannot accept\n" +
2019-10-10 15:40:18 +08:00
"extraneous leading spaces.")
```
```python
Yes:
2020-06-30 04:39:32 +08:00
long_string = ("And this too is fine if you cannot accept\n"
2019-10-10 15:40:18 +08:00
"extraneous leading spaces.")
```
```python
Yes:
import textwrap
long_string = textwrap.dedent("""\
This is also fine, because textwrap.dedent()
will collapse common leading spaces in each line.""")
2018-02-06 04:51:47 +08:00
```
2020-12-16 06:31:04 +08:00
< a id = "s3.10.1-logging" > < / a >
< a id = "3101-logging" > < / a >
< a id = "logging" > < / a >
< a id = "logging" > < / a >
#### 3.10.1 Logging
For logging functions that expect a pattern-string (with %-placeholders) as
their first argument: Always call them with a string literal (not an f-string!)
as their first argument with pattern-parameters as subsequent arguments. Some
logging implementations collect the unexpanded pattern-string as a queryable
field. It also prevents spending time rendering a message that no logger is
configured to output.
```python
Yes:
import tensorflow as tf
logger = tf.get_logger()
logger.info('TensorFlow Version is: %s', tf.__version__)
```
```python
Yes:
import os
from absl import logging
logging.info('Current $PAGER is: %s', os.getenv('PAGER', default=''))
homedir = os.getenv('HOME')
if homedir is None or not os.access(homedir, os.W_OK):
logging.error('Cannot write to home directory, $HOME=%r', homedir)
```
```python
No:
import os
from absl import logging
logging.info('Current $PAGER is:')
logging.info(os.getenv('PAGER', default=''))
homedir = os.getenv('HOME')
if homedir is None or not os.access(homedir, os.W_OK):
logging.error(f'Cannot write to home directory, $HOME={homedir!r}')
```
< a id = "s3.10.2-error-messages" > < / a >
< a id = "3102-error-messages" > < / a >
< a id = "error-messages" > < / a >
< a id = "error-messages" > < / a >
#### 3.10.2 Error Messages
Error messages (such as: message strings on exceptions like `ValueError` , or
messages shown to the user) should follow three guidelines:
1. The message needs to precisely match the actual error condition.
2. Interpolated pieces need to always be clearly identifiable as such.
3. They should allow simple automated processing (e.g. grepping).
```python
Yes:
if not 0 < = p < = 1:
raise ValueError(f'Not a probability: {p!r}')
try:
os.rmdir(workdir)
except OSError as error:
logging.warning('Could not remove directory (reason: %r): %r',
error, workdir)
```
```python
No:
if p < 0 or p > 1: # PROBLEM: also false for float('nan')!
raise ValueError(f'Not a probability: {p!r}')
try:
os.rmdir(workdir)
except OSError:
# PROBLEM: Message makes an assumption that might not be true:
# Deletion might have failed for some other reason, misleading
# whoever has to debug this.
logging.warning('Directory already was deleted: %s', workdir)
try:
os.rmdir(workdir)
except OSError:
# PROBLEM: The message is harder to grep for than necessary, and
# not universally non-confusing for all possible values of `workdir` .
# Imagine someone calling a library function with such code
# using a name such as workdir = 'deleted'. The warning would read:
# "The deleted directory could not be deleted."
logging.warning('The %s directory could not be deleted.', workdir)
```
2021-07-15 05:01:21 +08:00
< a id = "s3.11-files-sockets-closeables" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "s3.11-files-and-sockets" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "311-files-and-sockets" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "files-and-sockets" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "files" > < / a >
2021-07-15 05:01:21 +08:00
### 3.11 Files, Sockets, and similar Stateful Resources
2018-02-06 04:51:47 +08:00
2021-07-15 05:01:21 +08:00
Explicitly close files and sockets when done with them. This rule naturally
extends to closeable resources that internally use sockets, such as database
connections, and also other resources that need to be closed down in a similar
fashion. To name only a few examples, this also includes
[mmap ](https://docs.python.org/3/library/mmap.html ) mappings,
[h5py File objects ](https://docs.h5py.org/en/stable/high/file.html ), and
[matplotlib.pyplot figure windows ](https://matplotlib.org/2.1.0/api/_as_gen/matplotlib.pyplot.close.html ).
2018-02-06 04:51:47 +08:00
2021-07-15 05:01:21 +08:00
Leaving files, sockets or other such stateful objects open unnecessarily has
many downsides:
2018-02-06 04:51:47 +08:00
- They may consume limited system resources, such as file descriptors. Code
that deals with many such objects may exhaust those resources unnecessarily
if they're not returned to the system promptly after use.
2018-12-06 06:40:50 +08:00
- Holding files open may prevent other actions such as moving or deleting
2021-07-15 05:01:21 +08:00
them, or unmounting a filesystem.
2018-02-06 04:51:47 +08:00
- Files and sockets that are shared throughout a program may inadvertently be
read from or written to after logically being closed. If they are actually
2021-07-15 05:01:21 +08:00
closed, attempts to read or write from them will raise exceptions, making
2018-02-06 04:51:47 +08:00
the problem known sooner.
2021-07-15 05:01:21 +08:00
Furthermore, while files and sockets (and some similarly behaving resources) are
automatically closed when the object is destructed, coupling the lifetime of the
object to the state of the resource is poor practice:
2018-02-06 04:51:47 +08:00
2021-07-15 05:01:21 +08:00
- There are no guarantees as to when the runtime will actually invoke the
`__del__` method. Different Python implementations use different memory
management techniques, such as delayed garbage collection, which may
increase the object's lifetime arbitrarily and indefinitely.
2018-12-06 06:40:50 +08:00
- Unexpected references to the file, e.g. in globals or exception tracebacks,
may keep it around longer than intended.
2018-02-06 04:51:47 +08:00
2021-07-15 05:01:21 +08:00
Relying on finalizers to do automatic cleanup that has observable side effects
has been rediscovered over and over again to lead to major problems, across many
decades and multiple languages (see e.g.
[this article ](https://wiki.sei.cmu.edu/confluence/display/java/MET12-J.+Do+not+use+finalizers )
for Java).
The preferred way to manage files and similar resources is using the
2020-06-30 04:39:32 +08:00
[`with` statement ](http://docs.python.org/reference/compound_stmts.html#the-with-statement ):
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
with open("hello.txt") as hello_file:
for line in hello_file:
print(line)
```
2020-06-30 04:39:32 +08:00
For file-like objects that do not support the `with` statement, use
2018-02-06 04:51:47 +08:00
`contextlib.closing()` :
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
import contextlib
with contextlib.closing(urllib.urlopen("http://www.python.org/")) as front_page:
for line in front_page:
print(line)
```
2021-07-15 05:01:21 +08:00
In rare cases where context-based resource management is infeasible, code
documentation must explain clearly how resource lifetime is managed.
2018-02-06 04:51:47 +08:00
< a id = "s3.12-todo-comments" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "312-todo-comments" > < / a >
< a id = "todo" > < / a >
### 3.12 TODO Comments
2018-02-06 04:51:47 +08:00
Use `TODO` comments for code that is temporary, a short-term solution, or
good-enough but not perfect.
2018-12-06 06:40:50 +08:00
A `TODO` comment begins with the string `TODO` in all caps and a parenthesized
2018-02-06 04:51:47 +08:00
name, e-mail address, or other identifier
2018-12-06 06:40:50 +08:00
of the person or issue with the best context about the problem. This is followed
by an explanation of what there is to do.
The purpose is to have a consistent `TODO` format that can be searched to find
out how to get more details. A `TODO` is not a commitment that the person
referenced will fix the problem. Thus when you create a
`TODO` , it is almost always your name
that is given.
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
# TODO(kl@gmail.com): Use a "*" here for string repetition.
# TODO(Zeke) Change this to use relations.
```
If your `TODO` is of the form "At a future date do something" make sure that you
either include a very specific date ("Fix by November 2009") or a very specific
event ("Remove this code when all clients can handle XML responses.").
< a id = "s3.13-imports-formatting" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "313-imports-formatting" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "imports-formatting" > < / a >
2019-10-10 15:40:18 +08:00
### 3.13 Imports formatting
2018-02-06 04:51:47 +08:00
2020-06-30 04:39:32 +08:00
Imports should be on separate lines; there are
2022-04-12 01:17:08 +08:00
[exceptions for `typing` and `collections.abc` imports ](#typing-imports ).
2018-02-06 04:51:47 +08:00
E.g.:
2018-11-19 10:21:51 +08:00
```python
2022-06-08 07:37:30 +08:00
Yes: from collections.abc import Mapping, Sequence
import os
2018-02-06 04:51:47 +08:00
import sys
2022-06-08 07:37:30 +08:00
from typing import Any, NewType
2018-02-06 04:51:47 +08:00
```
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
No: import os, sys
```
2018-11-15 23:47:37 +08:00
2018-02-06 04:51:47 +08:00
Imports are always put at the top of the file, just after any module comments
and docstrings and before module globals and constants. Imports should be
2018-12-06 06:40:50 +08:00
grouped from most generic to least generic:
2018-02-06 04:51:47 +08:00
2019-10-10 15:40:18 +08:00
1. Python future import statements. For example:
```python
2022-05-26 03:15:47 +08:00
from __future__ import annotations
2019-10-10 15:40:18 +08:00
```
See [above ](#from-future-imports ) for more information about those.
2. Python standard library imports. For example:
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
import sys
```
2019-10-10 15:40:18 +08:00
3. [third-party ](https://pypi.org/ ) module
or package imports. For example:
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
import tensorflow as tf
```
2019-10-10 15:40:18 +08:00
4. Code repository
2018-02-06 04:51:47 +08:00
sub-package imports. For example:
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
from otherproject.ai import mind
```
2019-10-10 15:40:18 +08:00
5. **Deprecated:** application-specific imports that are part of the same
2018-02-06 04:51:47 +08:00
top level
sub-package as this file. For example:
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
from myproject.backend.hgwells import time_machine
```
2018-11-15 23:47:37 +08:00
You may find older Google Python Style code doing this, but it is no longer
2019-10-10 15:40:18 +08:00
required. **New code is encouraged not to bother with this.** Simply treat
application-specific sub-package imports the same as other sub-package
imports.
2018-11-15 23:47:37 +08:00
2018-02-06 04:51:47 +08:00
Within each grouping, imports should be sorted lexicographically, ignoring case,
2020-09-17 23:56:05 +08:00
according to each module's full package path (the `path` in `from path import
...`). Code may optionally place a blank line between import sections.
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
import collections
2018-11-15 23:47:37 +08:00
import queue
2018-02-06 04:51:47 +08:00
import sys
2018-11-15 23:47:37 +08:00
from absl import app
from absl import flags
import bs4
2018-02-06 04:51:47 +08:00
import cryptography
import tensorflow as tf
2018-11-15 23:47:37 +08:00
from book.genres import scifi
2020-09-17 23:56:05 +08:00
from myproject.backend import huxley
2018-11-15 23:47:37 +08:00
from myproject.backend.hgwells import time_machine
from myproject.backend.state_machine import main_loop
2018-02-06 04:51:47 +08:00
from otherproject.ai import body
from otherproject.ai import mind
from otherproject.ai import soul
2018-11-15 23:47:37 +08:00
# Older style code may have these imports down here instead:
#from myproject.backend.hgwells import time_machine
#from myproject.backend.state_machine import main_loop
2018-02-06 04:51:47 +08:00
```
< a id = "s3.14-statements" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "314-statements" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "statements" > < / a >
2019-10-10 15:40:18 +08:00
### 3.14 Statements
2018-02-06 04:51:47 +08:00
Generally only one statement per line.
However, you may put the result of a test on the same line as the test only if
the entire statement fits on one line. In particular, you can never do so with
`try` /`except` since the `try` and `except` can't both fit on the same line, and
you can only do so with an `if` if there is no `else` .
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes:
if foo: bar(foo)
```
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
No:
if foo: bar(foo)
else: baz(foo)
try: bar(foo)
except ValueError: baz(foo)
try:
bar(foo)
except ValueError: baz(foo)
```
2020-06-30 04:39:32 +08:00
< a id = "s3.15-accessors" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "s3.15-access-control" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "315-access-control" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "access-control" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "accessors" > < / a >
2021-09-13 13:42:39 +08:00
< a id = "getters-and-setters" > < / a >
### 3.15 Getters and Setters
Getter and setter functions (also called accessors and mutators) should be used
when they provide a meaningful role or behavior for getting or setting a
variable's value.
In particular, they should be used when getting or setting the variable is
complex or the cost is significant, either currently or in a reasonable future.
If, for example, a pair of getters/setters simply read and write an internal
attribute, the internal attribute should be made public instead. By comparison,
if setting a variable means some state is invalidated or rebuilt, it should be a
setter function. The function invocation hints that a potentially non-trivial
operation is occurring. Alternatively, [properties ](#properties ) may be an
option when simple logic is needed, or refactoring to no longer need getters and
setters.
Getters and setters should follow the [Naming ](#s3.16-naming ) guidelines, such
as `get_foo()` and `set_foo()` .
If the past behavior allowed access through a property, do not bind the new
getter/setter functions to the property. Any code still attempting to access the
variable by the old method should break visibly so they are made aware of the
change in complexity.
2018-02-06 04:51:47 +08:00
< a id = "s3.16-naming" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "316-naming" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "naming" > < / a >
2019-10-10 15:40:18 +08:00
### 3.16 Naming
`module_name` , `package_name` , `ClassName` , `method_name` , `ExceptionName` ,
`function_name` , `GLOBAL_CONSTANT_NAME` , `global_var_name` , `instance_var_name` ,
2022-04-12 01:17:08 +08:00
`function_parameter_name` , `local_var_name` , `query_proper_noun_for_thing` ,
`send_acronym_via_https` .
2019-10-10 15:40:18 +08:00
2018-02-06 04:51:47 +08:00
Function names, variable names, and filenames should be descriptive; eschew
abbreviation. In particular, do not use abbreviations that are ambiguous or
unfamiliar to readers outside your project, and do not abbreviate by deleting
letters within a word.
Always use a `.py` filename extension. Never use dashes.
< a id = "s3.16.1-names-to-avoid" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "3161-names-to-avoid" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "names-to-avoid" > < / a >
2019-10-10 15:40:18 +08:00
#### 3.16.1 Names to Avoid
2018-02-06 04:51:47 +08:00
2020-06-30 04:39:32 +08:00
- single character names, except for specifically allowed cases:
2018-02-06 04:51:47 +08:00
2020-09-17 23:56:05 +08:00
- counters or iterators (e.g. `i` , `j` , `k` , `v` , et al.)
2020-06-30 04:39:32 +08:00
- `e` as an exception identifier in `try/except` statements.
- `f` as a file handle in `with` statements
2022-04-12 01:17:08 +08:00
- private [`TypeVar`s ](#typing-type-var ) with no constraints (e.g. `_T` ,
`_U` , `_V` )
2019-10-10 15:40:18 +08:00
2020-06-30 04:39:32 +08:00
Please be mindful not to abuse single-character naming. Generally speaking,
descriptiveness should be proportional to the name's scope of visibility.
For example, `i` might be a fine name for 5-line code block but within
multiple nested scopes, it is likely too vague.
2018-02-06 04:51:47 +08:00
2020-06-30 04:39:32 +08:00
- dashes (`-`) in any package/module name
2018-02-06 04:51:47 +08:00
2020-06-30 04:39:32 +08:00
- `__double_leading_and_trailing_underscore__` names (reserved by Python)
2018-02-06 04:51:47 +08:00
2020-08-07 03:30:10 +08:00
- offensive terms
2020-12-16 06:31:04 +08:00
- names that needlessly include the type of the variable (for example:
`id_to_name_dict` )
2020-08-07 03:30:10 +08:00
< a id = "s3.16.2-naming-conventions" > < / a >
< a id = "3162-naming-convention" > < / a >
< a id = "naming-conventions" > < / a >
#### 3.16.2 Naming Conventions
- "Internal" means internal to a module, or protected or private within a
class.
- Prepending a single underscore (`_`) has some support for protecting module
2021-07-15 05:01:21 +08:00
variables and functions (linters will flag protected member access).
- Prepending a double underscore (`__` aka "dunder") to an instance variable
2020-08-07 03:30:10 +08:00
or method effectively makes the variable or method private to its class
2020-12-16 06:31:04 +08:00
(using name mangling); we discourage its use as it impacts readability and
2021-07-15 05:01:21 +08:00
testability, and isn't *really* private. Prefer a single underscore.
2020-08-07 03:30:10 +08:00
- Place related classes and top-level functions together in a
module.
2018-02-06 04:51:47 +08:00
Unlike Java, there is no need to limit yourself to one class per module.
- Use CapWords for class names, but lower\_with\_under.py for module names.
Although there are some old modules named CapWords.py, this is now
discouraged because it's confusing when the module happens to be named after
2018-12-06 06:40:50 +08:00
a class. ("wait -- did I write `import StringIO` or `from StringIO import
StringIO`?")
2018-02-06 04:51:47 +08:00
- Underscores may appear in *unittest* method names starting with `test` to
separate logical components of the name, even if those components use
CapWords. One possible pattern is `test<MethodUnderTest>_<state>` ; for
example `testPop_EmptyStack` is okay. There is no One Correct Way to name
test methods.
< a id = "s3.16.3-file-naming" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "3163-file-naming" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "file-naming" > < / a >
2019-10-10 15:40:18 +08:00
#### 3.16.3 File Naming
2018-02-06 04:51:47 +08:00
Python filenames must have a `.py` extension and must not contain dashes (`-`).
This allows them to be imported and unittested. If you want an executable to be
accessible without the extension, use a symbolic link or a simple bash wrapper
containing `exec "$0.py" "$@"` .
< a id = "s3.16.4-guidelines-derived-from-guidos-recommendations" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "3164-guidelines-derived-from-guidos-recommendations" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "guidelines-derived-from-guidos-recommendations" > < / a >
2020-06-30 04:39:32 +08:00
#### 3.16.4 Guidelines derived from [Guido](https://en.wikipedia.org/wiki/Guido_van_Rossum)'s Recommendations
2018-02-06 04:51:47 +08:00
< table rules = "all" border = "1" summary = "Guidelines from Guido's Recommendations"
cellspacing="2" cellpadding="2">
< tr >
< th > Type< / th >
< th > Public< / th >
< th > Internal< / th >
< / tr >
< tr >
< td > Packages< / td >
< td > < code > lower_with_under< / code > < / td >
< td > < / td >
< / tr >
< tr >
< td > Modules< / td >
< td > < code > lower_with_under< / code > < / td >
< td > < code > _lower_with_under< / code > < / td >
< / tr >
< tr >
< td > Classes< / td >
< td > < code > CapWords< / code > < / td >
< td > < code > _CapWords< / code > < / td >
< / tr >
< tr >
< td > Exceptions< / td >
< td > < code > CapWords< / code > < / td >
< td > < / td >
< / tr >
2018-06-17 11:58:16 +08:00
< tr >
2018-02-06 04:51:47 +08:00
< td > Functions< / td >
< td > < code > lower_with_under()< / code > < / td >
< td > < code > _lower_with_under()< / code > < / td >
< / tr >
< tr >
< td > Global/Class Constants< / td >
< td > < code > CAPS_WITH_UNDER< / code > < / td >
< td > < code > _CAPS_WITH_UNDER< / code > < / td >
< / tr >
< tr >
< td > Global/Class Variables< / td >
< td > < code > lower_with_under< / code > < / td >
< td > < code > _lower_with_under< / code > < / td >
< / tr >
< tr >
< td > Instance Variables< / td >
< td > < code > lower_with_under< / code > < / td >
< td > < code > _lower_with_under< / code > (protected)< / td >
< / tr >
2018-06-17 11:58:16 +08:00
< tr >
2018-02-06 04:51:47 +08:00
< td > Method Names< / td >
< td > < code > lower_with_under()< / code > < / td >
< td > < code > _lower_with_under()< / code > (protected)< / td >
< / tr >
< tr >
< td > Function/Method Parameters< / td >
< td > < code > lower_with_under< / code > < / td >
< td > < / td >
< / tr >
< tr >
< td > Local Variables< / td >
< td > < code > lower_with_under< / code > < / td >
< td > < / td >
< / tr >
< / table >
< a id = "s3.17-main" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "317-main" > < / a >
2021-05-07 06:53:45 +08:00
< a id = "math-notation" > < / a >
#### 3.16.5 Mathematical Notation
For mathematically heavy code, short variable names that would otherwise violate
the style guide are preferred when they match established notation in a
reference paper or algorithm. When doing so, reference the source of all naming
conventions in a comment or docstring or, if the source is not accessible,
clearly document the naming conventions. Prefer PEP8-compliant
`descriptive_names` for public APIs, which are much more likely to be
encountered out of context.
2018-02-06 04:51:47 +08:00
< a id = "main" > < / a >
2019-10-10 15:40:18 +08:00
### 3.17 Main
2018-02-06 04:51:47 +08:00
2020-09-17 23:56:05 +08:00
In Python, `pydoc` as well as unit tests require modules to be importable. If a
file is meant to be used as an executable, its main functionality should be in a
`main()` function, and your code should always check `if __name__ == '__main__'`
before executing your main program, so that it is not executed when the module
is imported.
2018-02-06 04:51:47 +08:00
2020-06-30 04:39:32 +08:00
When using [absl ](https://github.com/abseil/abseil-py ), use `app.run` :
```python
from absl import app
...
2021-07-15 05:01:21 +08:00
def main(argv: Sequence[str]):
2020-06-30 04:39:32 +08:00
# process non-flag arguments
...
if __name__ == '__main__':
app.run(main)
```
Otherwise, use:
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
def main():
...
if __name__ == '__main__':
main()
```
All code at the top level will be executed when the module is imported. Be
careful not to call functions, create objects, or perform other operations that
should not be executed when the file is being `pydoc` ed.
< a id = "s3.18-function-length" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "318-function-length" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "function-length" > < / a >
2019-10-10 15:40:18 +08:00
### 3.18 Function length
2018-02-06 04:51:47 +08:00
Prefer small and focused functions.
We recognize that long functions are sometimes appropriate, so no hard limit is
placed on function length. If a function exceeds about 40 lines, think about
whether it can be broken up without harming the structure of the program.
Even if your long function works perfectly now, someone modifying it in a few
months may add new behavior. This could result in bugs that are hard to find.
Keeping your functions short and simple makes it easier for other people to read
and modify your code.
You could find long and complicated functions when working with
2020-08-07 03:30:10 +08:00
some
code. Do not be intimidated by modifying existing code: if working with such a
function proves to be difficult, you find that errors are hard to debug, or you
want to use a piece of it in several different contexts, consider breaking up
the function into smaller and more manageable pieces.
2018-02-06 04:51:47 +08:00
< a id = "s3.19-type-annotations" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "319-type-annotations" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "type-annotations" > < / a >
2019-10-10 15:40:18 +08:00
### 3.19 Type Annotations
2018-02-06 04:51:47 +08:00
2020-06-30 04:39:32 +08:00
< a id = "s3.19.1-general-rules" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "s3.19.1-general" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "3191-general-rules" > < / a >
< a id = "typing-general" > < / a >
#### 3.19.1 General Rules
2018-02-06 04:51:47 +08:00
2020-08-07 03:30:10 +08:00
* Familiarize yourself with
[PEP-484 ](https://www.python.org/dev/peps/pep-0484/ ).
2022-05-26 03:15:47 +08:00
2020-08-07 03:30:10 +08:00
* In methods, only annotate `self` , or `cls` if it is necessary for proper
2022-05-26 03:15:47 +08:00
type information. e.g.,
```python
@classmethod
def create(cls: Type[T]) -> T:
return cls()
```
2022-04-12 01:17:08 +08:00
* Similarly, don't feel compelled to annotate the return value of `__init__`
(where `None` is the only valid option).
2022-05-26 03:15:47 +08:00
2020-08-07 03:30:10 +08:00
* If any other variable or a returned type should not be expressed, use `Any` .
2022-05-26 03:15:47 +08:00
2020-08-07 03:30:10 +08:00
* You are not required to annotate all the functions in a module.
2022-05-26 03:15:47 +08:00
2020-08-07 03:30:10 +08:00
- At least annotate your public APIs.
- Use judgment to get to a good balance between safety and clarity on the
one hand, and flexibility on the other.
- Annotate code that is prone to type-related errors (previous bugs or
complexity).
- Annotate code that is hard to understand.
- Annotate code as it becomes stable from a types perspective. In many
cases, you can annotate all the functions in mature code without losing
too much flexibility.
2018-02-06 04:51:47 +08:00
< a id = "s3.19.2-line-breaking" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "3192-line-breaking" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "typing-line-breaking" > < / a >
2019-10-10 15:40:18 +08:00
#### 3.19.2 Line Breaking
2018-02-06 04:51:47 +08:00
2018-11-15 23:47:37 +08:00
Try to follow the existing [indentation ](#indentation ) rules.
2018-02-06 04:51:47 +08:00
2018-11-15 23:47:37 +08:00
After annotating, many function signatures will become "one parameter per line".
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
def my_method(self,
first_var: int,
second_var: Foo,
third_var: Optional[Bar]) -> int:
...
```
2020-06-30 04:39:32 +08:00
Always prefer breaking between variables, and not, for example, between variable
names and type annotations. However, if everything fits on the same line, go for
it.
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
def my_method(self, first_var: int) -> int:
...
```
If the combination of the function name, the last parameter, and the return type
is too long, indent by 4 in a new line.
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
def my_method(
2022-04-12 01:17:08 +08:00
self, first_var: int) -> tuple[MyLongType1, MyLongType1]:
2018-02-06 04:51:47 +08:00
...
```
When the return type does not fit on the same line as the last parameter, the
preferred way is to indent the parameters by 4 on a new line and align the
2020-06-30 04:39:32 +08:00
closing parenthesis with the `def` .
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes:
def my_method(
2019-10-10 15:40:18 +08:00
self, other_arg: Optional[MyLongType]
2022-04-12 01:17:08 +08:00
) -> dict[OtherLongType, MyLongType]:
2018-02-06 04:51:47 +08:00
...
```
2020-08-07 03:30:10 +08:00
`pylint`
allows you to move the closing parenthesis to a new line and align with the
opening one, but this is less readable.
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
No:
def my_method(self,
2019-10-10 15:40:18 +08:00
other_arg: Optional[MyLongType]
2022-04-12 01:17:08 +08:00
) -> dict[OtherLongType, MyLongType]:
2018-02-06 04:51:47 +08:00
...
```
As in the examples above, prefer not to break types. However, sometimes they are
too long to be on a single line (try to keep sub-types unbroken).
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
def my_method(
self,
2022-04-12 01:17:08 +08:00
first_var: tuple[list[MyLongType1],
list[MyLongType2]],
second_var: list[dict[
2018-02-06 04:51:47 +08:00
MyLongType3, MyLongType4]]) -> None:
...
```
If a single name and type is too long, consider using an
[alias ](#typing-aliases ) for the type. The last resort is to break after the
colon and indent by 4.
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes:
def my_function(
long_variable_name:
long_module_name.LongTypeName,
) -> None:
...
```
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
No:
def my_function(
long_variable_name: long_module_name.
LongTypeName,
) -> None:
...
```
< a id = "s3.19.3-forward-declarations" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "3193-forward-declarations" > < / a >
< a id = "forward-declarations" > < / a >
#### 3.19.3 Forward Declarations
2018-02-06 04:51:47 +08:00
If you need to use a class name from the same module that is not yet defined --
for example, if you need the class inside the class declaration, or if you use a
2021-12-08 02:42:12 +08:00
class that is defined below -- either use `from __future__ import annotations`
for simple cases or use a string for the class name.
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2021-12-08 02:42:12 +08:00
from __future__ import annotations
2020-06-30 04:39:32 +08:00
class MyClass:
2018-02-06 04:51:47 +08:00
2021-12-08 02:42:12 +08:00
def __init__ (self, stack: Sequence[MyClass]) -> None:
2018-02-06 04:51:47 +08:00
```
< a id = "s3.19.4-default-values" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "3194-default-values" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "typing-default-values" > < / a >
2019-10-10 15:40:18 +08:00
#### 3.19.4 Default Values
2018-02-06 04:51:47 +08:00
2018-12-06 06:40:50 +08:00
As per
[PEP-008 ](https://www.python.org/dev/peps/pep-0008/#other-recommendations ), use
2021-12-08 02:42:12 +08:00
spaces around the `=` *only* for arguments that have both a type annotation and
2018-12-06 06:40:50 +08:00
a default value.
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes:
def func(a: int = 0) -> int:
...
```
2020-08-07 03:30:10 +08:00
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
No:
def func(a:int=0) -> int:
...
```
2020-06-30 04:39:32 +08:00
< a id = "s3.19.5-nonetype" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "s3.19.5-none-type" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "3195-nonetype" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "none-type" > < / a >
2019-10-10 15:40:18 +08:00
#### 3.19.5 NoneType
2018-02-06 04:51:47 +08:00
In the Python type system, `NoneType` is a "first class" type, and for typing
purposes, `None` is an alias for `NoneType` . If an argument can be `None` , it
has to be declared! You can use `Union` , but if there is only one other type,
2018-11-15 23:47:37 +08:00
use `Optional` .
Use explicit `Optional` instead of implicit `Optional` . Earlier versions of PEP
2021-05-07 06:53:45 +08:00
484 allowed `a: str = None` to be interpreted as `a: Optional[str] = None` , but
that is no longer the preferred behavior.
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
Yes:
2021-05-07 06:53:45 +08:00
def func(a: Optional[str], b: Optional[str] = None) -> str:
2018-11-15 23:47:37 +08:00
...
2021-05-07 06:53:45 +08:00
def multiple_nullable_union(a: Union[None, str, int]) -> str:
2018-02-06 04:51:47 +08:00
...
```
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
No:
2021-05-07 06:53:45 +08:00
def nullable_union(a: Union[None, str]) -> str:
2018-02-06 04:51:47 +08:00
...
2021-05-07 06:53:45 +08:00
def implicit_optional(a: str = None) -> str:
2018-02-06 04:51:47 +08:00
...
```
2020-06-30 04:39:32 +08:00
< a id = "s3.19.6-type-aliases" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "s3.19.6-aliases" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "3196-type-aliases" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "typing-aliases" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "type-aliases" > < / a >
#### 3.19.6 Type Aliases
2018-02-06 04:51:47 +08:00
You can declare aliases of complex types. The name of an alias should be
2020-08-07 03:30:10 +08:00
CapWorded. If the alias is used only in this module, it should be \_Private.
2018-02-06 04:51:47 +08:00
2019-10-10 15:40:18 +08:00
For example, if the name of the module together with the name of the type is too
long:
2018-02-06 04:51:47 +08:00
2022-06-08 07:37:30 +08:00
<!-- Annotate below with `typing.TypeAlias` for 3.10. -->
2018-11-19 10:21:51 +08:00
```python
2018-11-29 02:15:08 +08:00
_ShortName = module_with_long_name.TypeWithLongName
2022-04-12 01:17:08 +08:00
ComplexMap = Mapping[str, list[tuple[int, int]]]
2018-02-06 04:51:47 +08:00
```
Other examples are complex nested types and multiple return variables from a
function (as a tuple).
2020-06-30 04:39:32 +08:00
< a id = "s3.19.7-ignoring-types" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "s3.19.7-ignore" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "3197-ignoring-types" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "typing-ignore" > < / a >
2019-10-10 15:40:18 +08:00
#### 3.19.7 Ignoring Types
2018-02-06 04:51:47 +08:00
2020-08-07 03:30:10 +08:00
You can disable type checking on a line with the special comment `# type:
ignore`.
2018-02-06 04:51:47 +08:00
`pytype` has a disable option for specific errors (similar to lint):
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
# pytype: disable=attribute-error
```
2020-06-30 04:39:32 +08:00
< a id = "s3.19.8-typing-variables" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "s3.19.8-comments" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "3198-typing-internal-variables" > < / a >
< a id = "typing-variables" > < / a >
#### 3.19.8 Typing Variables
2018-02-06 04:51:47 +08:00
2020-06-30 04:39:32 +08:00
< a id = "annotated-assignments" > < / a >
2019-10-10 15:40:18 +08:00
[*Annotated Assignments* ](#annotated-assignments )
2022-04-19 16:30:32 +08:00
: If an internal variable has a type that is hard or impossible to infer,
specify its type with an annotated assignment - use a colon and type between
the variable name and value (the same as is done with function arguments
that have a default value):
2019-10-10 15:40:18 +08:00
2022-04-19 16:30:32 +08:00
```python
a: Foo = SomeUndecoratedFunction()
```
< a id = "type-comments" > < / a >
[*Type Comments* ](#type-comments )
: Though you may see them remaining in the codebase (they were necessary
before Python 3.6), do not add any more uses of a `# type: <type name>`
comment on the end of the line:
```python
a = SomeUndecoratedFunction() # type: Foo
```
2018-02-06 04:51:47 +08:00
2020-06-30 04:39:32 +08:00
< a id = "s3.19.9-tuples-vs-lists" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "s3.19.9-tuples" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "3199-tuples-vs-lists" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "typing-tuples" > < / a >
2019-10-10 15:40:18 +08:00
#### 3.19.9 Tuples vs Lists
2018-02-06 04:51:47 +08:00
2020-06-30 04:39:32 +08:00
Typed lists can only contain objects of a single type. Typed tuples can either
have a single repeated type or a set number of elements with different types.
The latter is commonly used as the return type from a function.
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2022-04-19 16:30:32 +08:00
a: list[int] = [1, 2, 3]
b: tuple[int, ...] = (1, 2, 3)
c: tuple[int, str, float] = (1, "2", 3.5)
2018-02-06 04:51:47 +08:00
```
2020-06-30 04:39:32 +08:00
< a id = "s3.19.10-typevars" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "s3.19.10-type-var" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "31910-typevar" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "typing-type-var" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "typevars" > < / a >
#### 3.19.10 TypeVars
2018-02-06 04:51:47 +08:00
The Python type system has
[generics ](https://www.python.org/dev/peps/pep-0484/#generics ). The factory
function `TypeVar` is a common way to use them.
Example:
2018-11-19 10:21:51 +08:00
```python
2022-04-12 01:17:08 +08:00
from typing import TypeVar
_T = TypeVar("_T")
2018-02-06 04:51:47 +08:00
...
2022-04-12 01:17:08 +08:00
def next(l: list[_T]) -> _T:
2018-02-06 04:51:47 +08:00
return l.pop()
```
A TypeVar can be constrained:
2018-11-19 10:21:51 +08:00
```python
2021-05-07 06:53:45 +08:00
AddableType = TypeVar("AddableType", int, float, str)
2018-02-06 04:51:47 +08:00
def add(a: AddableType, b: AddableType) -> AddableType:
return a + b
```
A common predefined type variable in the `typing` module is `AnyStr` . Use it for
2022-05-26 03:15:47 +08:00
multiple annotations that can be `bytes` or `str` and must all be the same type.
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2018-11-15 23:47:37 +08:00
from typing import AnyStr
def check_length(x: AnyStr) -> AnyStr:
if len(x) < = 42:
return x
raise ValueError()
2018-02-06 04:51:47 +08:00
```
2018-11-15 23:47:37 +08:00
2022-04-12 01:17:08 +08:00
A TypeVar must have a descriptive name, unless it meets all of the following
criteria:
* not externally visible
* not constrained
```python
Yes:
_T = TypeVar("_T")
AddableType = TypeVar("AddableType", int, float, str)
AnyFunction = TypeVar("AnyFunction", bound=Callable)
```
```python
No:
T = TypeVar("T")
_T = TypeVar("_T", int, float, str)
_F = TypeVar("_F", bound=Callable)
```
2020-06-30 04:39:32 +08:00
< a id = "s3.19.11-string-types" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "s3.19.11-strings" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "31911-string-types" > < / a >
2018-12-06 06:40:50 +08:00
2019-10-10 15:40:18 +08:00
< a id = "typing-strings" > < / a >
#### 3.19.11 String types
2018-02-06 04:51:47 +08:00
2022-05-26 03:15:47 +08:00
> Do not use `typing.Text` in new code. It's only for Python 2/3 compatibility.
2018-02-06 04:51:47 +08:00
2022-05-26 03:15:47 +08:00
Use `str` for string/text data. For code that deals with binary data, use
`bytes` .
2018-11-19 10:21:51 +08:00
```python
2022-05-26 03:15:47 +08:00
def deals_with_text_data(x: str) -> str:
2018-02-06 04:51:47 +08:00
...
2018-11-19 10:21:51 +08:00
def deals_with_binary_data(x: bytes) -> bytes:
2018-02-06 04:51:47 +08:00
...
```
If all the string types of a function are always the same, for example if the
return type is the same as the argument type in the code above, use
[AnyStr ](#typing-type-var ).
2020-06-30 04:39:32 +08:00
< a id = "s3.19.12-imports-for-typing" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "s3.19.12-imports" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "31912-imports-for-typing" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "typing-imports" > < / a >
2019-10-10 15:40:18 +08:00
#### 3.19.12 Imports For Typing
2018-02-06 04:51:47 +08:00
2022-05-26 03:15:47 +08:00
For symbols from the `typing` and `collections.abc` modules used to support
static analysis and type checking, always import the symbol itself. This keeps
common annotations more concise and matches typing practices used around the
world. You are explicitly allowed to import multiple specific classes on one
line from the `typing` and `collections.abc` modules. Ex:
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2022-04-12 01:17:08 +08:00
from collections.abc import Mapping, Sequence
from typing import Any, Union
2018-02-06 04:51:47 +08:00
```
2022-04-12 01:17:08 +08:00
Given that this way of importing adds items to the local namespace, names in
`typing` or `collections.abc` should be treated similarly to keywords, and not
be defined in your Python code, typed or not. If there is a collision between a
type and an existing name in a module, import it using `import x as y` .
2018-02-06 04:51:47 +08:00
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
from typing import Any as AnyType
```
2018-11-15 23:47:37 +08:00
< a id = "s3.19.13-conditional-imports" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "31913-conditional-imports" > < / a >
2018-11-15 23:47:37 +08:00
< a id = "typing-conditional-imports" > < / a >
2019-10-10 15:40:18 +08:00
#### 3.19.13 Conditional Imports
2018-11-15 23:47:37 +08:00
2018-12-06 06:40:50 +08:00
Use conditional imports only in exceptional cases where the additional imports
needed for type checking must be avoided at runtime. This pattern is
discouraged; alternatives such as refactoring the code to allow top level
imports should be preferred.
2018-02-06 04:51:47 +08:00
2020-08-07 03:30:10 +08:00
Imports that are needed only for type annotations can be placed within an `if
TYPE_CHECKING:` block.
2018-11-15 23:47:37 +08:00
2019-12-31 05:45:23 +08:00
- Conditionally imported types need to be referenced as strings, to be forward
compatible with Python 3.6 where the annotation expressions are actually
evaluated.
2018-02-06 04:51:47 +08:00
- Only entities that are used solely for typing should be defined here; this
includes aliases. Otherwise it will be a runtime error, as the module will
not be imported at runtime.
- The block should be right after all the normal imports.
- There should be no empty lines in the typing imports list.
2018-11-15 23:47:37 +08:00
- Sort this list as if it were a regular imports list.
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
import typing
if typing.TYPE_CHECKING:
2018-11-15 23:47:37 +08:00
import sketch
def f(x: "sketch.Sketch"): ...
2018-02-06 04:51:47 +08:00
```
2020-06-30 04:39:32 +08:00
< a id = "s3.19.14-circular-dependencies" > < / a >
2018-11-15 23:47:37 +08:00
< a id = "s3.19.14-circular-deps" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "31914-circular-dependencies" > < / a >
2018-02-06 04:51:47 +08:00
< a id = "typing-circular-deps" > < / a >
2019-10-10 15:40:18 +08:00
#### 3.19.14 Circular Dependencies
2018-02-06 04:51:47 +08:00
Circular dependencies that are caused by typing are code smells. Such code is a
good candidate for refactoring. Although technically it is possible to keep
2020-12-16 06:31:04 +08:00
circular dependencies, various build systems will not let you do so
because each module has to depend on the other.
2018-02-06 04:51:47 +08:00
Replace modules that create circular dependency imports with `Any` . Set an
[alias ](#typing-aliases ) with a meaningful name, and use the real type name from
this module (any attribute of Any is Any). Alias definitions should be separated
from the last import by one line.
2018-11-19 10:21:51 +08:00
```python
2018-02-06 04:51:47 +08:00
from typing import Any
some_mod = Any # some_mod.py imports this module.
...
2020-06-30 04:39:32 +08:00
def my_method(self, var: "some_mod.SomeType") -> None:
2018-02-06 04:51:47 +08:00
...
```
2018-11-15 23:47:37 +08:00
< a id = "typing-generics" > < / a >
< a id = "s3.19.15-generics" > < / a >
2019-10-10 15:40:18 +08:00
< a id = "31915-generics" > < / a >
2018-11-15 23:47:37 +08:00
2019-10-10 15:40:18 +08:00
< a id = "generics" > < / a >
#### 3.19.15 Generics
2018-11-15 23:47:37 +08:00
When annotating, prefer to specify type parameters for generic types; otherwise,
[the generics' parameters will be assumed to be `Any` ](https://www.python.org/dev/peps/pep-0484/#the-any-type ).
2018-11-19 10:21:51 +08:00
```python
2022-04-12 01:17:08 +08:00
def get_names(employee_ids: list[int]) -> dict[int, Any]:
2018-11-15 23:47:37 +08:00
...
```
2018-11-19 10:21:51 +08:00
```python
2022-04-12 01:17:08 +08:00
# These are both interpreted as get_names(employee_ids: list[Any]) -> dict[Any, Any]
2018-11-15 23:47:37 +08:00
def get_names(employee_ids: list) -> Dict:
...
def get_names(employee_ids: List) -> Dict:
...
```
If the best type parameter for a generic is `Any` , make it explicit, but
remember that in many cases [`TypeVar` ](#typing-type-var ) might be more
appropriate:
2018-11-19 10:21:51 +08:00
```python
2022-04-12 01:17:08 +08:00
def get_names(employee_ids: list[Any]) -> dict[Any, str]:
2018-11-15 23:47:37 +08:00
"""Returns a mapping from employee ID to employee name for given IDs."""
```
2018-11-19 10:21:51 +08:00
```python
2022-04-12 01:17:08 +08:00
_T = TypeVar('_T')
def get_names(employee_ids: list[_T]) -> dict[_T, str]:
2018-11-15 23:47:37 +08:00
"""Returns a mapping from employee ID to employee name for given IDs."""
```
2018-02-06 04:51:47 +08:00
2019-10-10 15:40:18 +08:00
< a id = "4-parting-words" > < / a >
< a id = "consistency" > < / a >
## 4 Parting Words
2018-02-06 04:51:47 +08:00
*BE CONSISTENT*.
If you're editing code, take a few minutes to look at the code around you and
determine its style. If they use spaces around all their arithmetic operators,
you should too. If their comments have little boxes of hash marks around them,
make your comments have little boxes of hash marks around them too.
The point of having style guidelines is to have a common vocabulary of coding so
people can concentrate on what you're saying rather than on how you're saying
it. We present global style rules here so people know the vocabulary, but local
style is also important. If code you add to a file looks drastically different
from the existing code around it, it throws readers out of their rhythm when
they go to read it. Avoid this.