Variables and Assignment Style Notes
Spaces are used sparingly in Python. It is common to put spaces
around the assignment operator. The recommended style is
c = (f-32)*5/9
Do not take great pains to line up assignment operators vertically.
The following has too much space, and is hard to read, even though it is
fussily aligned.
a = 12
b = a*math.log(a)
aVeryLongVariable = 26
d = 13
This is considered poor form because Python takes a lot of its look
from natural languages and mathematics. This kind of horizontal whitespace
is hard to follow: it can get difficult to be sure which expression lines
up with which variable. Python programs are meant to be reasonably
compact, more like reading a short narrative paragraph or short
mathematical formula than reading a page-sized UML diagram.
Variable names are often given as mixedCase;
variable names typically begin with lower-case letters. The
lower_case_with_underscores style is also used, but is
less popular.
In addition, the following special forms using leading or trailing
underscores are recognized:
-
single_trailing_underscore_: used to avoid
conflicts with Python keywords. For example: print_ =
42
-
__double_leading_and_trailing_underscore__:
used for special objects or attributes, e.g.
__init__, __dict__ or
__file__. These names are reserved; do not use
names like these in your programs unless you specifically mean a
particular built-in feature of Python.