Skip to content

Commit a26f75e

Browse files
author
Kenneth Reitz
committed
Merge pull request realpython#101 from kevinburke/style
Clean up the style page
2 parents bac7149 + d5a5e54 commit a26f75e

File tree

1 file changed

+183
-9
lines changed

1 file changed

+183
-9
lines changed

docs/writing/style.rst

Lines changed: 183 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ Also known as PEP 20, the guiding principles for Python's design.
9696
If the implementation is easy to explain, it may be a good idea.
9797
Namespaces are one honking great idea -- let's do more of those!
9898

99-
See `<http://stackoverflow.com/questions/228181/the-zen-of-python>`_ for some
100-
examples.
99+
For some examples of good Python style, see `this Stack Overflow question
100+
<http://stackoverflow.com/questions/228181/the-zen-of-python>`_ or `these
101+
slides from a Python user group
102+
<http://artifex.org/~hblanks/talks/2011/pep20_by_example.pdf>`_.
101103

102104
PEP 8
103105
-----
@@ -106,16 +108,18 @@ PEP 8 is the de-facto code style guide for Python.
106108

107109
`PEP 8 <http://www.python.org/dev/peps/pep-0008/>`_
108110

109-
There exists a command-line program, `pep8` that can check your code for
110-
conformance.
111+
Conforming your Python code to PEP 8 is generally a good idea and helps make
112+
code more consistent when working on projects with other developers. There
113+
exists a command-line program, `pep8 <https://github.com/jcrocholl/pep8>`_,
114+
that can check your code for conformance. Install it by running the following
115+
command in your Terminal:
111116

112117
::
113118

114-
pip install pep8
119+
$ pip install pep8
115120

116121

117-
Simply run it on a file or series of files and get a report of any
118-
violations
122+
Then run it on a file or series of files to get a report of any violations.
119123

120124
::
121125

@@ -129,5 +133,175 @@ violations
129133
optparse.py:472:29: E221 multiple spaces before operator
130134
optparse.py:544:21: W601 .has_key() is deprecated, use 'in'
131135

132-
Conforming your style to PEP 8 is generally a good idea and helps make code a lot
133-
more consistent when working on projects with other developers.
136+
Conventions
137+
:::::::::::
138+
139+
Here are some conventions you should follow to make your code easier to read.
140+
141+
Check if variable equals a constant
142+
-----------------------------------
143+
144+
You don't need to explicitly compare a value to True, or None, or 0 - you can
145+
just add it to the if statement.
146+
147+
**Bad**:
148+
149+
.. code-block:: python
150+
151+
if attr == True:
152+
print 'True!'
153+
154+
if attr == None:
155+
print 'attr is None!'
156+
157+
**Good**:
158+
159+
.. code-block:: python
160+
161+
# Just check the value
162+
if attr:
163+
print 'True!'
164+
165+
# or check for the opposite
166+
if not attr:
167+
print 'attr is None!'
168+
169+
Access a Dictionary Element
170+
---------------------------
171+
172+
Don't use the ``has_key`` function. Instead use ``x in d`` syntax, or pass
173+
a default argument to ``get``.
174+
175+
**Bad**:
176+
177+
.. code-block:: python
178+
179+
d = {'hello': 'world'}
180+
if d.has_key('hello'):
181+
print d['hello'] # prints 'world'
182+
else:
183+
print 'default_value'
184+
185+
**Good**:
186+
187+
.. code-block:: python
188+
189+
d = {'hello': 'world'}
190+
191+
print d.get('hello', 'default_value') # prints 'world'
192+
print d.get('thingy', 'default_value') # prints 'default_value'
193+
194+
# Or:
195+
if 'hello' in d:
196+
print d['hello']
197+
198+
Short Ways to Manipulate Lists
199+
------------------------------
200+
201+
`List comprehensions
202+
<http://docs.python.org/tutorial/datastructures.html#list-comprehensions>`_
203+
provide a powerful, concise way to work with lists. Also, the `map
204+
<http://docs.python.org/library/functions.html#map>`_ and `filter
205+
<http://docs.python.org/library/functions.html#filter>`_ functions can perform
206+
operations on lists using a different concise syntax.
207+
208+
**Bad**:
209+
210+
.. code-block:: python
211+
212+
# Filter elements less than 5
213+
a = [3, 4, 5]
214+
b = []
215+
for i in a:
216+
if a > 4:
217+
b.append(a)
218+
219+
**Good**:
220+
221+
.. code-block:: python
222+
223+
b = [i for i in a if i > 4]
224+
b = filter(lambda x: x > 4, a)
225+
226+
**Bad**:
227+
228+
.. code-block:: python
229+
230+
# Add three to all list members.
231+
a = [3, 4, 5]
232+
count = 0
233+
for i in a:
234+
a[count] = i + 3
235+
count = count + 1
236+
237+
**Good**:
238+
239+
.. code-block:: python
240+
241+
a = [3, 4, 5]
242+
a = [i + 3 for i in a]
243+
# Or:
244+
a = map(lambda i: i + 3, a)
245+
246+
Use `enumerate <http://docs.python.org/library/functions.html#enumerate>`_ to
247+
keep a count of your place in the list.
248+
249+
.. code-block:: python
250+
251+
for i, item in a:
252+
print i + ", " + item
253+
# prints
254+
# 0, 3
255+
# 1, 4
256+
# 2, 5
257+
258+
Read From a File
259+
----------------
260+
261+
Use the ``with open`` syntax to read from files. This will automatically close
262+
files for you.
263+
264+
**Bad**:
265+
266+
.. code-block:: python
267+
268+
f = open('file.txt')
269+
a = f.read()
270+
print a
271+
f.close()
272+
273+
**Good**:
274+
275+
.. code-block:: python
276+
277+
with open('file.txt') as f:
278+
for line in f:
279+
print line
280+
281+
Returning Multiple Values from a Function
282+
-----------------------------------------
283+
284+
Python supports returning multiple values from a function as a comma-separated
285+
list, so you don't have to create an object or dictionary and pack multiple
286+
values in before you return
287+
288+
**Bad**:
289+
290+
.. code-block:: python
291+
292+
def math_func(a):
293+
return {'square': a ** 2, 'cube': a ** 3}
294+
295+
d = math_func(3)
296+
s = d['square']
297+
c = d['cube']
298+
299+
**Good**:
300+
301+
.. code-block:: python
302+
303+
def math_func(a):
304+
return a ** 2, a ** 3
305+
306+
square, cube = math_func(3)
307+

0 commit comments

Comments
 (0)