“Readability counts.”
– Tim Peters, long-time Python contributor, The Zen of Python
You were probably taught to give your variables descriptive names, such as total = price + tax
, as opposed to t = p + tax
. But, sometimes, you are told there are traditional variable names, like
for i in range(1, 4): # i is the outer loop index
for j in range(1, 4): # j is the inner loop index
print(i, j)
Consider the following code with poor variable names and improper spacing:
def bs(a,x):
there=False
fst,lst = 0,len(a)-1
while fst<=lst and not there:
mid=(fst+lst)//2
if x<a[mid]:
lst=mid-1
elif x>a[mid]:
fst=mid+1
else:
return True
return False
As a developer, it would certainly take me a minute to figure out what this function does. Better names would go a long way for sure. But also, the improper spacing makes it needlessly difficult to see what each line is doing. In Python, every operator should have a single space around it. For example, lst=mid-1
should be lst = mid - 1
.
Now compare to a properly named, properly spaced solution:
def binary_search(lst, target):
found = False
first, last = 0, len(lst) - 1
while first <= last and not found:
mid = (first + last) // 2
if x < lst[mid]:
last = mid - 1
elif x > lst[mid]:
first = mid + 1
else:
return True
return False
Coding conventions are the rules for naming, spacing, and commenting adopted by an organization. These conventions are often language-specific. Google has coding conventions for many languages that they expect their developers to follow, for example. Many organizations will use their own conventions. One of the nice things about coding conventions is that they can be checked by tools in the IDE to let you know if you’re violating them.
The creators of Python have published a set of coding conventions for the whole language, called PEP 8 - Style Guide for Python Code, which we will follow in this class.
The sections below are a subset of the rules that I consider the most impactful on readability.
lowercase_with_underscores
only.compute_risk()
body_mass_index = 20.0
PascalCase
beginning with an uppercase letter, e.g., PatientRecord
lowercase
letters. You may use _
if it improves readability.spam(ham[1], {eggs: 2})
spam( ham[ 1 ] , { eggs: 2 } )
if x == 4: print(x, y); x, y = y, x
if x == 4 : print(x , y) ; x , y = y , x
Consistently applying coding conventions makes your code easier to understand.
We can use tools to help enforce coding conventions, and we will do so soon. For now, concentrate on learning the Python naming and spacing conventions above.
class patient:
def __init__(self,firstName,lastName,age):
self.firstName=firstName
self.lastName=lastName
self.age=age
def computeBill(self,fee,interest):
return fee*(1+interest)
def printRecord(self):
print(f"{self.firstName} {self.lastName} {self.age}")
if __name__ == "__main__":
bob = patient('bob', 'bobberton', 55)
bob.printRecord()