Skip to content

Instantly share code, notes, and snippets.

@mdeous
Created January 7, 2011 13:22
Show Gist options
  • Save mdeous/769445 to your computer and use it in GitHub Desktop.
Save mdeous/769445 to your computer and use it in GitHub Desktop.
python code conventions
# put builtin libs first (and direct imports before 'from' imports)
import os
import sys
from threading import Thread
# then put 3rd party imports
import feedparser
from BeautifulSoup import BeautifulSoup
# and then project internal imports
import myapp.core
from myapp.libs import mymodule
# constants are all uppercase
VERSION = '1.1'
BUFFER_SIZE = 32
# class names are capitalized words
class MyClass(object):
# method names are lowercase with underscores
def do_something_useful(self):
pass
# except when using a framework with other conventions (twisted for example)
def camelCaseFunction(self):
pass
# function names follow the same conventions as methods
def my_leet_function():
pass
# when testing for None, do it explicitely
if var is None:
pass
# when testing for 0-length objects, do this way:
var = []
if not var:
# var length is 0
pass
var = ''
if not var:
pass
# use isinstance to test an object's type
var = [1, 2, 3]
if isinstance(var, list):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment