Project

General

Profile

Python Coding Standards » History » Version 1

Neil Voss, 03/19/2010 02:11 PM

1 1 Neil Voss
h1. Python Coding Standards
2
3
This document is a list of python coding standards. To add a new standard copy the template below and modify it.
4
5
----
6
7
h2. +Name of Coding Standard+
8
9
h3. Definition
10
11
What is the coding standard
12
13
h3. Justification
14
15
Why is the coding standard important
16
17
h3. Example
18
19
GOOD:<pre>
20
this is a good example code
21
</pre>
22
23
BAD:<pre>
24
this is a bad example code
25
</pre>
26
27
----
28
29
h1. Python Coding Standards for AMI
30
31
----
32
33
h2. +Checking beginning or ending of strings+
34
35
h3. Definition
36
37
Use ''.startswith() and ''.endswith() instead of string slicing to check for prefixes or suffixes.
38
39
h3. Justification
40
41
startswith() and endswith() are cleaner and less error prone.
42
43
h3. Example
44
45
GOOD:<pre>
46
if foo.startswith('bar'): 
47
</pre>
48
49
BAD:<pre>
50
if foo[:3] == 'bar':
51
</pre>
52
53
----
54
55
h2. +Never use @from module import *@+
56
57
h3. Definition
58
59
Never use @from module import *@, use @import module@ instead
60
61
h3. Justification
62
63
It is hard to track where functions come from when @import *@ is used
64
65
h3. Example
66
67
GOOD:<pre>
68
import numpy
69
a = numpy.ones((3,3))
70
</pre>
71
72
BAD:<pre>
73
from numpy import *
74
a = ones((3,3))
75
</pre>