If you don't use it, you are dead to me.
- Avoid long lines and do not use a backslash for line continuation. Figure it out. Look at some of these examples for help:
zone_sums = dict(
one_free=Sum('ports__one_gig_available'),
one_total=Sum('ports__one_gig_total'),
ten_free=Sum('ports__ten_gig_available'),
ten_total=Sum('ports__ten_gig_total'),
)
zone_ports = (
self.devices.values('zone')
.filter(role='Switch')
.annotate(**zone_sums)
)
- Indent with 4 (or 2 - pick one) spaces per level. Never use tabs to indent.
- Always start a new block on its own line. i.e. avoid 'if foo: print foo'
- Don't import more than one module per import statement.
- Never use 'from module import * '! Namespaces make code easier to read so please use them.
- Name module level global constants in UPPERCASE letters using underscores between words, like ZONE_PATHS.
- Name booleans using is_ or has_ like is_active or has_spam.
- Use leading underscores in names that aren't meant to be used by others. _kitchen_size
- For naming functions or methods, use underscores between words. E.g. do_the_thing, or process_form.
- Module names should always be lowercase. E.g. almodevice, database, log.
- In argument lists, use (thing=value, ...) instead of (thing = value, ...), i.e. no whitespace there is preferred, but DO put a space after each comma (or a newline).
for foo, bar in foobars:
print bar, foo
- Class names should start with uppercase. E.g. AlmoDevice, Database.
- Don't include the type of a variable in its name. E.g. Use senders instead of senderlist.
- Place a class's __init__ at the top.
- For long comments, summarize on the first line, followed by a blank line then the remainder of the notes.
- When raising an exception, put the arguments in the call to the exception like so:
raise Exception("arguments", "go here", 14)
- Reuse existing Exceptions from Python instead of inventing your own.
- Be consistent with your use of quotes. Pick single-quotes or double-quotes and stick with your choice.
- Pass your code through a decent linter. I currently use flake8-linter in Atom.
- If your class has two methods, one of which is __init__, then you shouldn't be using a class.
- Always have a main function, that way the code can be imported as a module for testing, etc, i.e:
def main():
...
if __name__ == __main__:
main()
- Used named arguments to improve readability urlget('http://google.com', 20) vs. urlget('http://google.com', timeout=20)
- Watch this PyCon 2013 presentation from Brandon Rhodes. It's good! http://pyvideo.org/video/1676/the-naming-of-ducks-where-dynamic-types-meet-sma