Book of the Week: How to Make Mistakes in Python

15 Oct 2015

After you’ve learned a new programming language, it is important that you get caught up with best practices. This week, I read How to Make Mistakes in Python, because it was free. Most of the content you can read online, but it is nice to have a starting point in once place. The book is written from the point of view of a python web developer. Quick read and good reminder. Environment I can sometimes be difficult to know where each package is installed and the environment matching between your development environment and production environment. You can use virtualenv to setup a self-contained environment to install your python packages. As you development, you tend to collect packages. Jupyter and ipython provide a better REPL than the standard python REPL. You should use them to be more productive. Tab completing is very nice. Best Practices Optimize and check for errors with pylint. Follow a style guide such as pep8 or Google’s python style guide. Add logging. Use nose to run unittest. I also like doctest. Tips Try using strategy pattern instead of if else blocks. Don’t put list as default argument unless you know what you’re doing, because that list will be persistent through all the function calls. You’re not going to get an empty list every time you call the function. [sourcecode language=”python” wraplines=”false” collapse=”false”] def bad(arg=[]): pass def good(arg=None): if not arg: arg = [] [/sourcecode]