blob: 71a52f2b47dfee94efe986cb72eccc333fb50215 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001.. highlightlang:: rest
2
Alexander Belopolskyea3e91e2010-11-11 22:43:45 +00003Style guide
Georg Brandl116aa622007-08-15 14:28:22 +00004===========
5
6The Python documentation should follow the `Apple Publications Style Guide`_
7wherever possible. This particular style guide was selected mostly because it
8seems reasonable and is easy to get online.
9
Alexander Belopolskyea3e91e2010-11-11 22:43:45 +000010Topics which are either not covered in Apple's style guide or treated
11differently in Python documentation will be discussed in this
12document.
13
14Use of whitespace
15-----------------
Georg Brandl116aa622007-08-15 14:28:22 +000016
Georg Brandl2592f622011-03-20 18:14:07 +000017All reST files use an indentation of 3 spaces; no tabs are allowed. The
18maximum line length is 80 characters for normal text, but tables, deeply
19indented code samples and long links may extend beyond that. Code example
20bodies should use normal Python 4-space indentation.
Georg Brandl116aa622007-08-15 14:28:22 +000021
22Make generous use of blank lines where applicable; they help grouping things
23together.
24
25A sentence-ending period may be followed by one or two spaces; while reST
26ignores the second space, it is customarily put in by some users, for example
27to aid Emacs' auto-fill mode.
28
Alexander Belopolskyea3e91e2010-11-11 22:43:45 +000029Footnotes
30---------
31
Georg Brandl116aa622007-08-15 14:28:22 +000032Footnotes are generally discouraged, though they may be used when they are the
33best way to present specific information. When a footnote reference is added at
34the end of the sentence, it should follow the sentence-ending punctuation. The
35reST markup should appear something like this::
36
37 This sentence has a footnote reference. [#]_ This is the next sentence.
38
39Footnotes should be gathered at the end of a file, or if the file is very long,
40at the end of a section. The docutils will automatically create backlinks to
41the footnote reference.
42
43Footnotes may appear in the middle of sentences where appropriate.
44
Alexander Belopolskyea3e91e2010-11-11 22:43:45 +000045Capitalization
46--------------
47
48.. sidebar:: Sentence case
49
50 Sentence case is a set of capitalization rules used in English
51 sentences: the first word is always capitalized and other words are
52 only capitalized if there is a specific rule requiring it.
53
54Apple style guide recommends the use of title case in section titles.
55However, rules for which words should be capitalized in title case
56vary greaty between publications.
57
58In Python documentation, use of sentence case in section titles is
59preferable, but consistency within a unit is more important than
60following this rule. If you add a section to the chapter where most
61sections are in title case you can either convert all titles to
62sentence case or use the dominant style in the new section title.
63
64Sentences that start with a word for which specific rules require
65starting it with a lower case letter should be avoided in titles and
66elsewhere.
67
68.. note::
69
70 Sections that describe a library module often have titles in the
71 form of "modulename --- Short description of the module." In this
72 case, the description should be capitalized as a stand-alone
73 sentence.
74
Georg Brandl116aa622007-08-15 14:28:22 +000075Many special names are used in the Python documentation, including the names of
76operating systems, programming languages, standards bodies, and the like. Most
77of these entities are not assigned any special markup, but the preferred
78spellings are given here to aid authors in maintaining the consistency of
79presentation in the Python documentation.
80
81Other terms and words deserve special mention as well; these conventions should
82be used to ensure consistency throughout the documentation:
83
84CPU
Alexander Belopolskyea3e91e2010-11-11 22:43:45 +000085 For "central processing unit." Many style guides say this should be
86 spelled out on the first use (and if you must use it, do so!). For
87 the Python documentation, this abbreviation should be avoided since
88 there's no reasonable way to predict which occurrence will be the
89 first seen by the reader. It is better to use the word "processor"
90 instead.
Georg Brandl116aa622007-08-15 14:28:22 +000091
92POSIX
Alexander Belopolskyea3e91e2010-11-11 22:43:45 +000093 The name assigned to a particular group of standards. This is always
94 uppercase.
Georg Brandl116aa622007-08-15 14:28:22 +000095
96Python
Alexander Belopolskyea3e91e2010-11-11 22:43:45 +000097 The name of our favorite programming language is always capitalized.
98
99reST
100 For "reStructuredText," an easy to read, plaintext markup syntax
101 used to produce Python documentation. When spelled out, it is
102 always one word and both forms start with a lower case 'r'.
Georg Brandl116aa622007-08-15 14:28:22 +0000103
104Unicode
Alexander Belopolskyea3e91e2010-11-11 22:43:45 +0000105 The name of a character coding system. This is always written
106 capitalized.
Georg Brandl116aa622007-08-15 14:28:22 +0000107
108Unix
Alexander Belopolskyea3e91e2010-11-11 22:43:45 +0000109 The name of the operating system developed at AT&T Bell Labs in the early
110 1970s.
Georg Brandl116aa622007-08-15 14:28:22 +0000111
Raymond Hettingerbb1cf8d2011-05-10 00:35:03 -0700112Affirmative Tone
113----------------
114
115The documentation focuses on affirmatively stating what the language does and
116how to use it effectively.
117
118Except for certain security risks or segfault risks, the docs should avoid
119wording along the lines of "feature x is dangerous" or "experts only". These
120kinds of value judgments belong in external blogs and wikis, not in the core
121documentation.
122
123Bad example (creating worry in the mind of a reader):
124
125 Warning: failing to explicitly close a file could result in lost data or
126 excessive resource consumption. Never rely on reference counting to
127 automatically close a file.
128
129Good example (establishing confident knowledge in the effective use of the language):
130
131 A best practice for using files is use a try/finally pair to explicitly
132 close a file after it is used. Alternatively, using a with-statement can
133 achieve the same effect. This assures that files are flushed and file
134 descriptor resources are released in a timely manner.
135
136Economy of Expression
137---------------------
138
Éric Araujod4701f42011-05-25 18:13:29 +0200139More documentation is not necessarily better documentation. Err on the side
Raymond Hettingerbb1cf8d2011-05-10 00:35:03 -0700140of being succinct.
141
142It is an unfortunate fact that making documentation longer can be an impediment
143to understanding and can result in even more ways to misread or misinterpret the
144text. Long descriptions full of corner cases and caveats can create the
145impression that a function is more complex or harder to use than it actually is.
146
147The documentation for :func:`super` is an example of where a good deal of
148information was condensed into a few short paragraphs. Discussion of
149:func:`super` could have filled a chapter in a book, but it is often easier to
150grasp a terse description than a lengthy narrative.
151
152
153Code Examples
154-------------
155
156Short code examples can be a useful adjunct to understanding. Readers can often
157grasp a simple example more quickly than they can digest a formal description in
158prose.
159
160People learn faster with concrete, motivating examples that match the context of
161a typical use case. For instance, the :func:`str.rpartition` method is better
162demonstrated with an example splitting the domain from a URL than it would be
163with an example of removing the last word from a line of Monty Python dialog.
164
165The ellipsis for the :attr:`sys.ps2` secondary interpreter prompt should only be
166used sparingly, where it is necessary to clearly differentiate between input
167lines and output lines. Besides contributing visual clutter, it makes it
168difficult for readers to cut-and-paste examples so they can experiment with
169variations.
170
171Code Equivalents
172----------------
173
174Giving pure Python code equivalents (or approximate equivalents) can be a useful
175adjunct to a prose description. A documenter should carefully weigh whether the
176code equivalent adds value.
177
178A good example is the code equivalent for :func:`all`. The short 4-line code
179equivalent is easily digested; it re-emphasizes the early-out behavior; and it
180clarifies the handling of the corner-case where the iterable is empty. In
181addition, it serves as a model for people wanting to implement a commonly
182requested alternative where :func:`all` would return the specific object
183evaluating to False whenever the function terminates early.
184
185A more questionable example is the code for :func:`itertools.groupby`. Its code
186equivalent borders on being too complex to be a quick aid to understanding.
187Despite its complexity, the code equivalent was kept because it serves as a
188model to alternative implementations and because the operation of the "grouper"
189is more easily shown in code than in English prose.
190
191An example of when not to use a code equivalent is for the :func:`oct` function.
192The exact steps in converting a number to octal doesn't add value for a user
193trying to learn what the function does.
194
195Audience
196--------
197
198The tone of the tutorial (and all the docs) needs to be respectful of the
199reader's intelligence. Don't presume that the readers are stupid. Lay out the
200relevant information, show motivating use cases, provide glossary links, and do
Éric Araujod4701f42011-05-25 18:13:29 +0200201your best to connect-the-dots, but don't talk down to them or waste their time.
Raymond Hettingerbb1cf8d2011-05-10 00:35:03 -0700202
203The tutorial is meant for newcomers, many of whom will be using the tutorial to
204evaluate the language as a whole. The experience needs to be positive and not
205leave the reader with worries that something bad will happen if they make a
206misstep. The tutorial serves as guide for intelligent and curious readers,
207saving details for the how-to guides and other sources.
208
209Be careful accepting requests for documentation changes from the rare but vocal
210category of reader who is looking for vindication for one of their programming
211errors ("I made a mistake, therefore the docs must be wrong ..."). Typically,
212the documentation wasn't consulted until after the error was made. It is
213unfortunate, but typically no documentation edit would have saved the user from
214making false assumptions about the language ("I was surprised by ...").
215
Georg Brandl116aa622007-08-15 14:28:22 +0000216
Georg Brandl386bc6d2010-04-25 10:19:53 +0000217.. _Apple Publications Style Guide: http://developer.apple.com/mac/library/documentation/UserExperience/Conceptual/APStyleGuide/APSG_2009.pdf
Georg Brandl116aa622007-08-15 14:28:22 +0000218