-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10-Python-style-guide.html
More file actions
144 lines (144 loc) · 5.65 KB
/
10-Python-style-guide.html
File metadata and controls
144 lines (144 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<title>Software Carpentry: Building programs with Python</title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap-theme.css" />
<link rel="stylesheet" type="text/css" href="css/swc.css" />
<link rel="alternate" type="application/rss+xml" title="Software Carpentry Blog" href="http://software-carpentry.org/feed.xml"/>
<meta charset="UTF-8" />
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body class="lesson">
<div class="container card">
<div class="banner">
<a href="http://www.ecs.soton.ac.uk" title="Electronics and Computer Science">
<img alt="ECS banner" src="img/ecs-logo.png" />
</a>
</div>
<article>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<a href="index.html"><h1 class="title">Building programs with Python</h1></a>
<h2 class="subtitle">Python Style Guide</h2>
<section class="objectives panel panel-warning">
<div class="panel-heading">
<h2 id="learning-objectives"><span class="glyphicon glyphicon-certificate"></span>Learning Objectives</h2>
</div>
<div class="panel-body">
<ul>
<li>Python coding conventions</li>
</ul>
</div>
</section>
<p>This is taken from <a href="https://www.python.org/dev/peps/pep-0008/">PEP-008: Python Style Guide</a>. It is a semi-official guide to Python coding conventions.</p>
<p>We should stick to this unless we have hard data that proves something else is better.</p>
<p>Basic layout is as below:</p>
<ul>
<li>Indent blocks using four spaces</li>
<li>Keep lines less than 80 characters long</li>
<li>Separate functions with two blank lines</li>
<li>Separate logical chunks of long functions with a single blank line</li>
<li>Put comments on lines of their own, rather than to the right of code</li>
</ul>
<p>Here are some basic python style rules listed in a table below:</p>
<table style="width:58%;">
<colgroup>
<col width="20%" />
<col width="19%" />
<col width="18%" />
</colgroup>
<thead>
<tr class="header">
<th><strong>Rule</strong></th>
<th><strong>Good</strong></th>
<th><strong>Bad</strong></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>No whitespace immediately inside parentheses or before the parenthesis starting indexing or slicing</td>
<td><code>max(candidates[sublist])</code></td>
<td><code>max( candidates[ sublist ] )</code> , <code>max (candidates [sublist] )</code></td>
</tr>
<tr class="even">
<td>No whitespace immediately before comma or colon</td>
<td><code>if limit > 0: print minimum, limit</code></td>
<td><code>if limit > 0 : print minimum , limit</code></td>
</tr>
<tr class="odd">
<td>Use space around arithmetic and in-place operators</td>
<td><code>x += 3 * 5</code></td>
<td><code>x+=3*5</code></td>
</tr>
<tr class="even">
<td>No spaces when specifying default parameter values</td>
<td><code>def integrate(func, start=0.0, interval=1.0)</code></td>
<td><code>def integrate(func, start = 0.0, interval = 1.0)</code></td>
</tr>
<tr class="odd">
<td>Never use names that are distinguished only by <code>"l"</code>, <code>"1"</code>, <code>"0"</code>, or <code>"O"</code></td>
<td><code>tempo_long</code> and <code>tempo_init</code></td>
<td><code>tempo_l</code> and <code>tempo_1</code></td>
</tr>
<tr class="even">
<td>Short lower-case names for modules (i.e., files)</td>
<td><code>geology</code></td>
<td><code>Geology</code> or <code>geology_package</code></td>
</tr>
<tr class="odd">
<td>Upper case with underscores for constants</td>
<td><code>TOLERANCE</code> or <code>MAX_AREA</code></td>
<td><code>Tolerance</code> or <code>MaxArea</code></td>
</tr>
<tr class="even">
<td>Camel case for class names</td>
<td><code>SingleVariableIntegrator</code></td>
<td><code>single_variable_integrator</code></td>
</tr>
<tr class="odd">
<td>Lowercase with underscores for function and method names</td>
<td><code>divide_region</code></td>
<td><code>divRegion</code></td>
</tr>
<tr class="even">
<td>and member variables</td>
<td><code>max_so_far</code></td>
<td><code>maxSoFar</code></td>
</tr>
<tr class="odd">
<td>Use <code>is</code> and <code>is not</code> when comparing to special values</td>
<td><code>if current is not None:</code></td>
<td><code>if current != None:</code></td>
</tr>
<tr class="even">
<td>Use <code>isinstance</code> when checking types</td>
<td><code>if isinstance(current, Rock):</code></td>
<td><code>if type(current) == Rock:</code></td>
</tr>
</tbody>
</table>
<p align="center">
<pre><code> <strong>Table 8.1: Basic Python Style Rules </strong> </code></pre>
</p>
</div>
</div>
</article>
<div class="footer">
This work is derived from prior works that are Copyright © <a href="http://software-carpentry.org">Software Carpentry</a>
<a class="label swc-blue-bg" href="LICENSE.html">License</a>
<a class="label swc-blue-bg" href="https://github.com/Southampton-RSG/2016-08-31-Southampton">Source</a>
</div>
</div>
<!-- Javascript placed at the end of the document so the pages load faster -->
<script src="http://software-carpentry.org/v5/js/jquery-1.9.1.min.js"></script>
<script src="css/bootstrap/bootstrap-js/bootstrap.js"></script>
</body>
</html>