Revision 2.59
Amit PatelEach style point has a summary for which additional information is available by toggling the accompanying arrow button that looks this way:
. You may toggle all summaries with the big arrow button:Python is the main scripting language used at Google. This style guide is a list of dos and don'ts for Python programs.
To help you format code correctly, we've created a settings file for Vim. For Emacs, the default settings should be fine.
string
module
where possible. Use function call syntax instead
of apply
. Use list comprehensions
and for
loops instead of filter
and
map
when the function argument would have been an
inlined lambda anyway. Use for
loops instead of
reduce
.
.py
files do not need to start with a
#!
line. Start the main file of a
program with
#!/usr/bin/python
with an optional single digit
2
or 3
suffix per
PEP-394.
object
. This also applies to nested classes.
format
method or the %
operator for
formatting strings, even when the parameters are all strings. Use your
best judgement to decide between +
and %
(or format
) though.
TODO
comments for code that is temporary, a
short-term solution, or good-enough but not perfect.
TODO
s should include the string TODO
in
all caps, followed by the
name, e-mail address, or other
identifier
of the person who can best provide context about the problem
referenced by the TODO
,
in parentheses. A colon is optional. A comment explaining what there
is to do is required. The main purpose is to have
a consistent TODO
format that can be searched to find the
person who can provide more details upon request. 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.
# 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.").
property
to keep the syntax consistent.
module_name, package_name, ClassName,
method_name, ExceptionName,
function_name, GLOBAL_CONSTANT_NAME,
global_var_name, instance_var_name, function_parameter_name,
local_var_name.
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.
Revision 2.59
Amit Patel