Spot any errors? let me know, but Unleash your pedant politely please.

Wednesday 17 June 2009

Code Like a Pythonista: Idiomatic Python

Code Like a Pythonista: Idiomatic Python by David Goodger.

It's a must read. I was surprised at how much I already knew, but it's a great reference, and it has lots of good links at the bottom too.

I wish I'd read this a few months ago, when I started dabbling in Python. I didn't realise how deep I'd get. I though just up to the ankles, but I'm maybe up to my knees by now.

Things I've done right :
  • Used 4 spaces for indents. I always have, in all languages that allowed it. (Though possibly not in examples on this blog, for reasons of laziness)

  • Tried to stick to 80 chars. This is just a habit from my days (and nights) on a VT220.

  • StudlyCaps for classes.

  • Avoided single line compound statements.

  • Practicality beats Purity. Always believed this. Some people try to write optimised code. I try to write readable code, and if it needs to be optimised, refactor later, and litter the code with comments explaining why.


Things I'm naturally graduating toward:
  • I love whitespace. I use tons of it. Always have. It really helps readability. So I may have as many as 10 blank lines between functions. Gradually, this has been reducing. While I'm using Python, the excess whitespace has become annoying. I'm not quite at the 1 line between functions, 2 between classes guideline, but I think I'll naturally get there soon.
    Used None for many default parameters, initialising new objects inside functions. (I didn't really know why, but now I do, and I can fix it where I've got it wrong at the moment)


Things I've done wrong:
  • Used camelCase where I should be using joined_lower. I prefer camelCase, but if joined_lower is more Pythonic, I'm willing to yield this. Not sure if I'll change all the code that I already have though.

  • Not used spaces after , and : in lists and dicts

  • Not used docstrings much (My excuse is that I'm in a mixed Java/Jython environment, and I don't know how to make the docstrings useful).

  • I have some unnecessary "== True" or "== None" gubbins. The linked doc has a table explaining how the Truth Values work.

No comments:

Post a Comment