From 4025990529e2d98908df1cf8d72627e4d70e9658 Mon Sep 17 00:00:00 2001
From: bhumijgupta PEP 526 introduced variable annotations. The style recommendations for them are similar to those on function annotations described above: Yes: No: Make sure to indent the continued line appropriately. For decades the recommended style was to break after binary operators. But this can hurt readability in two ways: the operators tend to get scattered across different columns on the screen, and each operator is moved away from its operand and onto the previous line. Here, the eye has to do extra work to tell which items are added and which are subtracted: Imports should be grouped in the following order: You should put a blank line between each group of imports. Trailing commas are usually optional, except they are mandatory when making a tuple of one element (and in Python 2 they have semantics for the Note that there is a separate convention for builtin names: most builtin names are single words (or two words run together), with the CapWords convention used only for exception names and builtin constants. Names of type variables introduced in PEP 484 should normally use CapWords
@@ -923,7 +923,7 @@ or contravariant behavior correspondingly. Examples Modules that are designed for use via Function names should be lowercase, with words separated by underscores as necessary to improve readability.
+
+Variable annotations
+
+
+
+
+
+code: int
+
+class Point:
+ coords: Tuple[int, int]
+ label: str = '<unknown>'
+code:int # No space after colon
+code : int # Space before colon
+
+class Test:
+ result: int=0 # No spaces around equality sign
Footnotes
From df27e7f2afe3bf4afecedc418c46fd05b05f1951 Mon Sep 17 00:00:00 2001
From: bhumijgupta Should a line break before or after a binary operator?
+Should a Line Break Before or After a Binary Operator?
-
When to use trailing commas
+When to Use Trailing Commas
print statement). For clarity, it is recommended to surround the latter in (technically redundant) parentheses.Type variable names
+Type Variable Names
from M import * should use the __all__ mechanism to prevent exporting globals, or use the older convention of prefixing such globals with an underscore (which you might want to do to indicate these globals are “module non-public”).Function and variable names
+Function and Variable Names
mixedCase is allowed only in contexts where that’s already the prevailing style (e.g. threading.py), to retain backwards compatibility.
-Always use self for the first argument to instance methods.
Always decide whether a class’s methods and instance variables (collectively: “attributes”) should be public or non-public. If in doubt, choose non-public; it’s easier to make it public later than to make a public attribute non-public.
-Public attributes are those that you expect unrelated clients of your class to use, with your commitment to avoid backward incompatible changes. Non-public attributes are those that are not intended to be used by third parties; you make no guarantees that non-public attributes won’t change or even be removed.
+Public attributes are those that you expect unrelated clients of your class to use, with your commitment to avoid backwards incompatible changes. Non-public attributes are those that are not intended to be used by third parties; you make no guarantees that non-public attributes won’t change or even be removed.
We don’t use the term “private” here, since no attribute is really private in Python (without a generally unnecessary amount of work).
@@ -1004,7 +1004,7 @@ or contravariant behavior correspondingly. Examples -Any backwards compatibility guarantees apply only to public interfaces. Accordingly, it is important that users be able to clearly distinguish between public and internal interfaces.
From 9c135231c3b30245fbfd9f3b4c1d505911a67b7f Mon Sep 17 00:00:00 2001 From: bhumijguptaImports should usually be on separate lines, e.g.:
+Imports should usually be on separate lines
Yes:
import os
@@ -474,7 +474,7 @@ from .sibling import example
from myclass import MyClass
from foo.bar.yourclass import YourClass
-If this spelling causes local name clashes, then spell them :
+If this spelling causes local name clashes, then spell them explicitly :
import myclass
import foo.bar.yourclass
@@ -486,12 +486,10 @@ import foo.bar.yourclass
When republishing names this way, the guidelines below regarding public and internal interfaces still apply.
Module level "dunders" (i.e. names with two leading and two trailing underscores) such as __all__, __author__, __version__, etc. should be placed after the module docstring but before any import statements except from __future__ imports. Python mandates that future-imports must appear in the module before any other code except docstrings.
For example:
-"""This is the example module.
This module does stuff.
@@ -786,7 +784,7 @@ initialize(FILES, error=True,)
def line.PEP 257 describes good docstring conventions. Note that most importantly, the """ that ends a multiline docstring should be on a line by itself, e.g.:
PEP 257 describes good docstring conventions. Note that most importantly, the """ that ends a multiline docstring should be on a line by itself:
"""Return a foobang
@@ -901,7 +899,7 @@ Optional plotz says to frobnicate the bizbaz first.
Names of type variables introduced in PEP 484 should normally use CapWords
preferring short names: T, AnyStr, Num. It is recommended to add
suffixes _co or _contra to the variables used to declare covariant
-or contravariant behavior correspondingly. Examples
+or contravariant behavior correspondingly.
from typing import TypeVar
@@ -963,7 +961,7 @@ or contravariant behavior correspondingly. Examples
Constants are usually defined on a module level and written in all capital letters with underscores separating words. Examples include MAX_OVERFLOW and TOTAL.
-Designing for inheritance
+Designing for Inheritance
Always decide whether a class’s methods and instance variables (collectively: “attributes”) should be public or non-public. If in doubt, choose non-public; it’s easier to make it public later than to make a public attribute non-public.
@@ -1079,8 +1077,6 @@ or contravariant behavior correspondingly. Examples
When catching exceptions, mention specific exceptions whenever possible instead of using a bare except: clause.
-For example, use:
-
try:
import platform_specific_module
except ImportError:
@@ -1130,7 +1126,7 @@ except KeyError:
When a resource is local to a particular section of code, use a with statement to ensure it is cleaned up promptly and reliably after use. A try/finally statement is also acceptable.
-Context managers should be invoked through separate functions or methods whenever they do something other than acquire and release resources. For example:
+Context managers should be invoked through separate functions or methods whenever they do something other than acquire and release resources.
Yes:
@@ -1176,7 +1172,7 @@ def bar(x):
Use ''.startswith() and ''.endswith() instead of string slicing to check for prefixes or suffixes.
-startswith() and endswith() are cleaner and less error prone. For example:
+startswith() and endswith() are cleaner and less error prone.
Yes:
@@ -1257,7 +1253,7 @@ if not len(seq):
-Variable annotations
+Variable Annotations
PEP 526 introduced variable annotations. The style recommendations for them are similar to those on function annotations described above:
@@ -1274,7 +1270,7 @@ if not len(seq):
class Point:
coords: Tuple[int, int]
- label: str = '<unknown>'
+ label: str = '<unknown>'
No:
From c133f755af73331696c8a096bcee9524876778b0 Mon Sep 17 00:00:00 2001 From: bhumijguptaDon’t use spaces around the = sign when used to indicate a keyword argument or a default parameter value.
Yes:
- -def complex(real, imag=0.0):
- return magic(r=real, i=imag)
-
-No:
- -def complex(real, imag = 0.0):
- return magic(r = real, i = imag)Function annotations should use the normal rules for colons and always have spaces around the -> arrow if present. (See Function Annotations below for more about function annotations.)
Yes:
@@ -654,7 +642,19 @@ def munge() -> AnyStr: ...def munge(input:AnyStr): ...
def munge()->PosInt: ...When combining an argument annotation with a default value, use spaces around the = sign (but only for those arguments that have both an annotation and a default).
Don’t use spaces around the = sign when used to indicate a keyword argument,or when used to indicate a default value for an unannotated function parameter.
Yes:
+ +def complex(real, imag=0.0):
+ return magic(r=real, i=imag)
+
+No:
+ +def complex(real, imag = 0.0):
+ return magic(r = real, i = imag)When combining an argument annotation with a default value, however, do use spaces around the = sign:
Yes:
From 0c4a2e157421aa96d322c64ffd1317683b15bdb0 Mon Sep 17 00:00:00 2001 From: bhumijguptaThe default wrapping in most tools disrupts the visual structure of the code, making it more difficult to understand. The limits are chosen to avoid wrapping in editors with the window width set to 80, even if the tool places a marker glyph in the final column when wrapping lines. Some web based tools may not offer dynamic line wrapping at all.
-Some teams strongly prefer a longer line length. For code maintained exclusively or primarily by a team that can reach agreement on this issue, it is okay to increase the nominal line length from 80 to 100 characters (effectively increasing the maximum length to 99 characters), provided that comments and docstrings are still wrapped at 72 characters.
+Some teams strongly prefer a longer line length. For code maintained exclusively or primarily by a team that can reach agreement on this issue, it is okay to increase the line length limit up to 99 characters, provided that comments and docstrings are still wrapped at 72 characters.
The Python standard library is conservative and requires limiting lines to 79 characters (and docstrings/comments to 72).
From ea897599a1d7448a572968273089bcbb3658f2ed Mon Sep 17 00:00:00 2001 From: bhumijgupta_single_leading_underscore: weak “internal use” indicator. E.g. from M import * does not import objects whose name starts with an underscore._single_leading_underscore: weak “internal use” indicator. E.g. from M import * does not import objects whose name start with an underscore.single_trailing_underscore_: used by convention to avoid conflicts with Python keyword, e.g.: