These style guidelines are based on the Python PEP 8. To consult the Document go to: http://www.python.org/dev/peps/pep-0008/
my_name.py
It is desirable to do this:
import module_name as moduleName
(lower camelCase)
MyClass()
(CamelCase plus parenthesis)
myFunction()
(lower camelCase plus parenthesis)
myVariable
(lower camelCase)
Use 4 space-tabs per indentation level.
We prefer tabs. In any case, never mix tabs and spaces.
Yes:
import os
import sys
NO:
import os, sys
it's okay to say this though:
from subprocess import Popen, PIPE
You should put a blank line between each group of imports.
Put any relevant __all__ specification after the imports.
from myclass import MyClass
from foo.bar.yourclass import YourClass
If this spelling causes local name clashes, then spell them:
<<<<<<< .mineimport myclass
import foo.bar.yourclass
and use myclass.MyClass
and foo.bar.yourclass.YourClass
import myclass
import foo.bar.yourclass
and usemyclass.MyClass
andfoo.bar.yourclass.YourClass
Avoid extraneous whitespace in the following situations:
Yes:
spam(ham[1], {eggs: 2})
NO:
spam( ham[ 1 ], { eggs: 2 } )
Yes:
if x == 4: print x, y; x, y = y, x
NO:
if x == 4 : print x , y ; x , y = y , x
Yes:
spam(1)
NO:
spam (1)
Yes:
dict['key'] = list[index]
NO:
dict ['key'] = list [index]
Yes:
x = 1
y = 2
long_variable = 3
NO (the underscores represent whitespace):
x_____________= 1
y_____________= 2
long_variable_= 3
Yes:
i = i + 1
submitted += 1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)
NO:
i=i+1
submitted +=1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)
Yes (the underscores represent whitespace):
def complex(real, imag=0.0):
____return magic(r=real, i=imag)
NO (the underscores represent whitespace):
def complex(real, imag = 0.0):
____return magic(r = real, i = imag)
Yes (the underscores represent whitespace):
if foo == 'blah':
____do_blah_thing()
do_one()
do_two()
do_three()
Rather not:
if foo == 'blah': do_blah_thing()
do_one(); do_two(); do_three()
Rather not:
if foo == 'blah': do_blah_thing()
for x in lst: total += x
while t < 10: t = delay()
Definitely not:
if foo == 'blah': do_blah_thing()
else: do_non_blah_thing()
try: something()
finally: cleanup()
do_one(); do_two(); do_three (long, argument, list, like, this)
if foo == 'blah': one(); two(); three()
Don't do this:
x = x + 1 # Increment x
But sometimes, this is useful:
x = x + 1 # Compensate for border
""" This is an example docstring. The closing quotations are on an independent line.
"""
def myFunction(word, dictionary):
""" This unnecesary function looks for a word in a dictionary.
word__________---> a string to be looked-up in a dictionary
dictionary____---> a python dictionary
return________-->> the word's entry
"""