What are the best python programming patterns and practices you are using every day?

I don't mean only design patterns (there are lot of OO design patterns that can be used in python), I mean pure coding patterns and practices like:

  • the best way to organize modules
  • the best way for data parsing (performance wise)
  • usage of private functions (does this make sense, because you can access private functions outside the class)
  • what are the best modules for GUI, database etc.

Are there any new books or other kind of literature on this matter (2010/2011)?

Of course question can not be answered in one answer or one book, I thought of this question like a discussion on this matter because there is not really much written about python programming patterns and it is a very powerful language.



Tests : unit tests, integration tests and functional tests are the way you must do your job.

The most surprising is unit tests is far more complex to do than others because you need to create a code structure that let you replace your dependencies on the framework by fake implementation:

For exemple you can check this code: https://github.com/collective/collective.gallery/tree/master/collective/gallery/tests

utils.py contains fake components I next use in the setup to be sure I only use THE class I'm testing.


I'm using 'egg' to package my code and make it easy to use. So you can publish your add-on on the website http://pypi.python.org

To create an egg you can use PasteScript: http://pypi.python.org/pypi/PasteScript

To understand packaging you have to understand this:

  • distutils is just the setup.py file to say what are your dependencies and how to build & install the package
  • setuptools add the easy_install command with pypi as default egg repository (kind of apt-get). Today there is a fork of setuptools named distribute.
  • buildout add a way to get multiple version of the same eggs and it build the system path inside 'scripts' put in a bin folder: http://www.buildout.org/


No matter what performances are never checked first and this is bad. We are working on performances when there are performances issues.

http://pypi.python.org/pypi/funkload is a great tool to create performance tests.

Python provide tools to profile your code. I let you google that.


Never hide sth to other developers, useless and boring for them. 'property' make your public variable wrapped by accessor&mutator and do the job you want (never broke things)



I'm use to http://pypi.python.org/pypi/psycopg2 with postgresql. Python embed the sqlite binding since 2.5 to build tests

You should also consider that the http://zodb.org/ZODB can be used outside of Zope framework and is one of the oldest nosql database on python.


My favorite one: http://www.packtpub.com/expert-python-programming/book

Favorties packages & tools

Not the answer you're looking for? Browse other questions tagged or ask your own question.