A Question Of Python Style

Posted on Sun 11 October 2009 in misc

Having ended up almost solely with Python at the end of a journey of many languages I have learnt and used; style has been something important to me.

Python has pep8 which states something's definitively such as:-

  • Use 4 spaces per indentation level" (I was actually using 2 until now!)
  • Limit all lines to a maximum of 79 characters.
  • Separate top-level function and class definitions with two blank lines.
  • Method definitions inside a class are separated by a single blank line.

...so things like naming styles are purely at the discretion of the user. Fair enough but i like to be consistent where I can with these things, and coming from a Java background I pretty much stick with a style I picked up from there. Here's some explanation. Things always seemed very well defined in the land of Sun!

http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html#367

http://geosoft.no/development/javastyle.html

...and so

  • a class is ClassName()

  • method names are lookBehindYou()

  • class attributes are as above

this one from the geosoft link above I like also

  • "Abbreviations and acronyms should not be uppercase when used as name.

  • exportHtmlSource(); // NOT: exportHTMLSource();

  • openDvdPlayer(); // NOT: openDVDPlayer();"

I've not mentioned doc strings here, the use of pep8 tool, pylint or pyflakes or even given an example class, well I guess that's the beauty of blogging!

This is worth knowing though http://wiki.laptop.org/go/Python_Style_Guide oooh and removing trailing whitespace in vim... that's :%s/\s+$//