Skip to content

Commit d5a5e54

Browse files
author
Kevin Burke
committed
add examples of python idioms.
1 parent 07ace6f commit d5a5e54

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed

docs/writing/style.rst

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,175 @@ Then run it on a file or series of files to get a report of any violations.
7575
optparse.py:472:29: E221 multiple spaces before operator
7676
optparse.py:544:21: W601 .has_key() is deprecated, use 'in'
7777

78+
Conventions
79+
:::::::::::
80+
81+
Here are some conventions you should follow to make your code easier to read.
82+
83+
Check if variable equals a constant
84+
-----------------------------------
85+
86+
You don't need to explicitly compare a value to True, or None, or 0 - you can
87+
just add it to the if statement.
88+
89+
**Bad**:
90+
91+
.. code-block:: python
92+
93+
if attr == True:
94+
print 'True!'
95+
96+
if attr == None:
97+
print 'attr is None!'
98+
99+
**Good**:
100+
101+
.. code-block:: python
102+
103+
# Just check the value
104+
if attr:
105+
print 'True!'
106+
107+
# or check for the opposite
108+
if not attr:
109+
print 'attr is None!'
110+
111+
Access a Dictionary Element
112+
---------------------------
113+
114+
Don't use the ``has_key`` function. Instead use ``x in d`` syntax, or pass
115+
a default argument to ``get``.
116+
117+
**Bad**:
118+
119+
.. code-block:: python
120+
121+
d = {'hello': 'world'}
122+
if d.has_key('hello'):
123+
print d['hello'] # prints 'world'
124+
else:
125+
print 'default_value'
126+
127+
**Good**:
128+
129+
.. code-block:: python
130+
131+
d = {'hello': 'world'}
132+
133+
print d.get('hello', 'default_value') # prints 'world'
134+
print d.get('thingy', 'default_value') # prints 'default_value'
135+
136+
# Or:
137+
if 'hello' in d:
138+
print d['hello']
139+
140+
Short Ways to Manipulate Lists
141+
------------------------------
142+
143+
`List comprehensions
144+
<http://docs.python.org/tutorial/datastructures.html#list-comprehensions>`_
145+
provide a powerful, concise way to work with lists. Also, the `map
146+
<http://docs.python.org/library/functions.html#map>`_ and `filter
147+
<http://docs.python.org/library/functions.html#filter>`_ functions can perform
148+
operations on lists using a different concise syntax.
149+
150+
**Bad**:
151+
152+
.. code-block:: python
153+
154+
# Filter elements less than 5
155+
a = [3, 4, 5]
156+
b = []
157+
for i in a:
158+
if a > 4:
159+
b.append(a)
160+
161+
**Good**:
162+
163+
.. code-block:: python
164+
165+
b = [i for i in a if i > 4]
166+
b = filter(lambda x: x > 4, a)
167+
168+
**Bad**:
169+
170+
.. code-block:: python
171+
172+
# Add three to all list members.
173+
a = [3, 4, 5]
174+
count = 0
175+
for i in a:
176+
a[count] = i + 3
177+
count = count + 1
178+
179+
**Good**:
180+
181+
.. code-block:: python
182+
183+
a = [3, 4, 5]
184+
a = [i + 3 for i in a]
185+
# Or:
186+
a = map(lambda i: i + 3, a)
187+
188+
Use `enumerate <http://docs.python.org/library/functions.html#enumerate>`_ to
189+
keep a count of your place in the list.
190+
191+
.. code-block:: python
192+
193+
for i, item in a:
194+
print i + ", " + item
195+
# prints
196+
# 0, 3
197+
# 1, 4
198+
# 2, 5
199+
200+
Read From a File
201+
----------------
202+
203+
Use the ``with open`` syntax to read from files. This will automatically close
204+
files for you.
205+
206+
**Bad**:
207+
208+
.. code-block:: python
209+
210+
f = open('file.txt')
211+
a = f.read()
212+
print a
213+
f.close()
214+
215+
**Good**:
216+
217+
.. code-block:: python
218+
219+
with open('file.txt') as f:
220+
for line in f:
221+
print line
222+
223+
Returning Multiple Values from a Function
224+
-----------------------------------------
225+
226+
Python supports returning multiple values from a function as a comma-separated
227+
list, so you don't have to create an object or dictionary and pack multiple
228+
values in before you return
229+
230+
**Bad**:
231+
232+
.. code-block:: python
233+
234+
def math_func(a):
235+
return {'square': a ** 2, 'cube': a ** 3}
236+
237+
d = math_func(3)
238+
s = d['square']
239+
c = d['cube']
240+
241+
**Good**:
242+
243+
.. code-block:: python
244+
245+
def math_func(a):
246+
return a ** 2, a ** 3
247+
248+
square, cube = math_func(3)
249+

0 commit comments

Comments
 (0)