Read up on Python standards: http://legacy.python.org/dev/peps/pep-0008/
See this great presentation about unicode: Unicode and Python Pain
Project Specific Styles
Where possible, it is recommended to use the pop-0008 style guide for writing Python. However, in this project, here are a few extra adviseries:
-
use two (2) lines between functions and classes.
-
put python imports at top, followed by Django, followed by project-specific imports, each separated by one (1) line. For example, this might look like this:
import os from django.conf import settings from apps.generator.views import GenerateAcrosticFormView
-
for string formatting, use
format()
rather than%
or+
concatenation (see below for when to use+
, see this post:print("The {0} eats the {1}.".format('dog', 'cat'))
-
for string concatenation, use
+
for joining two (2) strings. If more than two (2), usejoin()
, see this post:string = substring + '\n' string = ''.join([self.name, '\n', self.part_of_speech, '\n', 'tags:\n'])
-
use single quotes for characters and small strings that don't make up intelligible sentences. Use double quotes for strings that should read like sentences, eg, error messages, alerts to user, etc. See this post.:
component_words = self.horizontal_words.split(';') print("DEBUG: error rendering view.")
-
use augmented assignment statements where possible:
sequence = sequence + 1 # not good sequence += 1 # good
-
comments should be double spaced after end of code line and have one space between pound sign and comment:
print("fatal error: please try again!") # error message