<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>the dam</title><link href="http://thibaut.horel.org/blog/" rel="alternate"></link><link href="http://thibaut.horel.org/blog/feeds/all.atom.xml" rel="self"></link><id>http://thibaut.horel.org/blog/</id><updated>2013-04-15T17:53:00+02:00</updated><entry><title>Using Vim as a Python IDE</title><link href="http://thibaut.horel.org/blog/using-vim-as-a-python-ide.html" rel="alternate"></link><updated>2013-04-15T17:53:00+02:00</updated><author><name>Thibaut</name></author><id>tag:thibaut.horel.org/blog,2013-04-15:using-vim-as-a-python-ide.html</id><summary type="html">&lt;p&gt;About one year ago, I have switched to using Vim as my main text and code
editor. Before that, I was using Emacs, but never really mastered it to the
point where I could consider myself proficient. After having heard a lot about
the &lt;em&gt;steep learning curve&lt;/em&gt; of Vim, I was surprised to find myself picking it up
rather quickly: I like the fact that shortcuts are structured similarly to
natural languages. People often mention how combining action and motion
instructions in Vim is similar to constructing a sentence in a natural
language, which I find an accurate comparison.&lt;/p&gt;
&lt;p&gt;Vim really shines when it comes to quickly editing a few files from the command
line, but even though I am not a heavy coder, I found the default configuration
a bit insufficient for bigger projects involving editing multiple related
files. Here is how I gradually tweaked my Vim configuration to copy some
behaviors commonly found in IDEs. While some of these tweaks are general and
can be applied to any language, others are specific to Python, the language
I use most of the time.&lt;/p&gt;
&lt;p&gt;This article assumes that you already have a basic knowledge of Vim and its
configuration. If you do not want to read my comments and prefer to stick to
the bare configuration steps, you can jump straight to the &lt;a class="reference internal" href="#conclusion"&gt;Conclusion&lt;/a&gt;.&lt;/p&gt;
&lt;div class="section" id="forewords"&gt;
&lt;h2&gt;Forewords&lt;/h2&gt;
&lt;p&gt;First and foremost, you should make sure that you have &lt;a class="reference external" href="https://github.com/tpope/vim-pathogen"&gt;Pathogen&lt;/a&gt; installed. This
amazing script allows you to handle your Vim plugins separately: each plugin
becomes a subdirectory of the &lt;tt class="docutils literal"&gt;.vim/bundle&lt;/tt&gt; folder (this can be changed to
somewhere else) in which you put all the plugin files instead of spreading them
over the default directory structure. Most of the plugins I will recommend here
are available on GitHub, so cloning their repositories inside your
&lt;tt class="docutils literal"&gt;.vim/bundle&lt;/tt&gt; folder is all you need to install them.&lt;/p&gt;
&lt;p&gt;This is particularly useful if you use Git to handle your Vim configuration
(and more generally your dotfiles), because you can treat each Vim plugin as
a submodule of your main repository, allowing for easy updates.&lt;/p&gt;
&lt;p&gt;&lt;a class="reference external" href="https://github.com/tpope/vim-pathogen#installation"&gt;Installing&lt;/a&gt; Pathogen is as easy running:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;mkdir -p ~/.vim/bundle ~/.vim/autoload
curl -Sso ~/.vim/autoload/pathogen.vim &lt;span class="se"&gt;\&lt;/span&gt;
    https://raw.github.com/tpope/vim-pathogen/master/autoload/pathogen.vim
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;and adding the following line at the top of your &lt;tt class="docutils literal"&gt;.vimrc&lt;/tt&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;execute pathogen#infect&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Also, as a general recommendation, make sure that you have a color theme that
match your tastes. Because I find dark themes more easy on the eyes, I went for
&lt;a class="reference external" href="https://github.com/tomasr/molokai"&gt;Molokai&lt;/a&gt;. &lt;a class="reference external" href="https://github.com/altercation/vim-colors-solarized"&gt;Solarized&lt;/a&gt; is a common choice among light themes lovers.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="file-and-project-management"&gt;
&lt;h2&gt;File and project management&lt;/h2&gt;
&lt;p&gt;When working on a project involving more than one file, being able to quickly
navigate through its files is key. &lt;a class="reference external" href="https://github.com/scrooloose/nerdtree"&gt;NERDTree&lt;/a&gt; is a nice plugin which replaces
the default Vim file browser and adds a file-tree drawer on the left of Vim's
interface. I bind the &lt;tt class="docutils literal"&gt;&amp;lt;F1&amp;gt;&lt;/tt&gt; key to the &lt;tt class="docutils literal"&gt;NERDTreeToogle&lt;/tt&gt; function to
quickly toggle the drawer on and off:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nb"&gt;nnoremap&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;F1&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; :NERDTreeToggle&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;cr&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I find the NERDTree plugin very useful when discovering a project, to quickly
grasp how the files are organized. However, to quickly open new files and
switch to open buffers, there is a much more powerful solution, I name the
&lt;a class="reference external" href="https://github.com/kien/ctrlp.vim"&gt;CtrlP&lt;/a&gt; plugin. This plugin adds commands to search through files, buffers,
tags, etc. in a fuzzy way. Let's say you want to open a file, pressing
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;&amp;lt;Ctrl-p&amp;gt;&lt;/span&gt;&lt;/tt&gt; followed by a few letters of this file's path (they don't need to
be contiguous) will give you a list of all the files matching these letters.
The list gets further filtered as you keep typing. When the file you are
looking for reaches the bottom of the list, you simply press &lt;tt class="docutils literal"&gt;&amp;lt;Enter&amp;gt;&lt;/tt&gt; to open
it.&lt;/p&gt;
&lt;p&gt;In addition to the default &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;&amp;lt;Ctrl-p&amp;gt;&lt;/span&gt;&lt;/tt&gt; shortcut to open new files, I like to
have a shortcut to launch the buffer finding command to search through open
buffers. Here I bind it to &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;&amp;lt;Ctrl-b&amp;gt;&lt;/span&gt;&lt;/tt&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nb"&gt;nnoremap&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="k"&gt;b&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; :CtrlPBuffer&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;cr&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div class="line-block"&gt;
&lt;div class="line"&gt;&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Vim has a very nice built-in &lt;a class="reference external" href="http://vimdoc.sourceforge.net/htmldoc/usr_21.html#21.4"&gt;session feature&lt;/a&gt;: you can save the current state
of your Vim instance (open buffers, windows layout, cursor positions, paste
registers, jumps, macros, etc.) in a session file. The &lt;a class="reference external" href="https://github.com/vim-scripts/sessionman.vim"&gt;sessionman&lt;/a&gt; plugin is
a simple wrapper around this feature, and takes care of storing all your
session files in the same directory. This is achieved by using the
&lt;tt class="docutils literal"&gt;SessionSave&lt;/tt&gt; command which asks you for a simple name to identify the
session. Call the &lt;tt class="docutils literal"&gt;SessionOpen&lt;/tt&gt; command with the same name to re-open the
session. Also, if you are working inside a previously saved session, the plugin
automatically saves the current state in the same session file when quitting
Vim.&lt;/p&gt;
&lt;p&gt;Finally, I would also recommend reading the &lt;a class="reference external" href="http://vimdoc.sourceforge.net/htmldoc/windows.html#buffers"&gt;buffers and windows sections&lt;/a&gt; of
Vim's help. Having a good understanding of buffers and windows, splits, etc.
can help you being more productive when working on multiple files.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="code-writing"&gt;
&lt;h2&gt;Code writing&lt;/h2&gt;
&lt;p&gt;There are many plugins to help you writing code more efficiently. Here are some
that I find particularly helpful.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p class="first"&gt;&lt;a class="reference external" href="https://github.com/majutsushi/tagbar"&gt;TagBar&lt;/a&gt;: to display a list of the current file's tags (when programming,
these are the functions, classes, global variables defined in the file) in
a drawer on the right of Vim's interface. This allows you to have a clear
view of the file content and to quickly jump to another location. I bind the
&lt;tt class="docutils literal"&gt;&amp;lt;F2&amp;gt;&lt;/tt&gt; key to the &lt;tt class="docutils literal"&gt;TagbarToggle&lt;/tt&gt; command to toggle the drawer:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nb"&gt;nnoremap&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;F2&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; :TagbarToggle&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;cr&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;note that this plugin requires that you have a &lt;tt class="docutils literal"&gt;ctags&lt;/tt&gt; implementation
installed on your computer.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;&lt;a class="reference external" href="https://github.com/tpope/vim-surround"&gt;Surround&lt;/a&gt;: gives you new motion commands to manipulate surrounding
parentheses, brackets and html tags. For example, let's say you are currently
editing some HTML code, you are inside a &lt;tt class="docutils literal"&gt;&amp;lt;span&amp;gt;&lt;/tt&gt; tag and realize that you
want to turn it into a &lt;tt class="docutils literal"&gt;&amp;lt;div&amp;gt;&lt;/tt&gt; tag. With this plugin this can be achieved
by typing &lt;tt class="docutils literal"&gt;csttdiv&lt;/tt&gt;, where &lt;tt class="docutils literal"&gt;cst&lt;/tt&gt; means &lt;em&gt;change surrounding tag&lt;/em&gt;, the
second &lt;tt class="docutils literal"&gt;t&lt;/tt&gt; means &lt;em&gt;replace it with another tag&lt;/em&gt;, and &lt;tt class="docutils literal"&gt;div&lt;/tt&gt; is simply the
name of the replacing tag.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;&lt;a class="reference external" href="https://github.com/kien/rainbow_parentheses.vim"&gt;RainbowParentheses&lt;/a&gt;: to color parentheses, brackets, etc. according to their
nesting level. This is quite helpful to quickly check the well-balancedness
of a complex expression. I bind the &lt;tt class="docutils literal"&gt;&amp;lt;F3&amp;gt;&lt;/tt&gt; key to toggle the rainbow colors
on and off:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nb"&gt;nnoremap&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;F3&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; :RainbowParenthesesToggleAll&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;cr&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Other plugins worth mentioning: &lt;a class="reference external" href="https://github.com/scrooloose/nerdcommenter"&gt;NERDCommenter&lt;/a&gt;, &lt;a class="reference external" href="https://github.com/Lokaltog/vim-easymotion"&gt;Easymotion&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="python-specific-configuration"&gt;
&lt;h2&gt;Python specific configuration&lt;/h2&gt;
&lt;p&gt;Python being a very popular language, there is a large ecosystem of tools to
help Python developers checking, formatting, refactoring and doing static
analysis of their code. If you do not want to spend too much time understanding
and installing all these tools, a coherent sample of them has been bundled into
the &lt;a class="reference external" href="https://github.com/klen/python-mode"&gt;python-mode&lt;/a&gt; plugin.&lt;/p&gt;
&lt;p&gt;I find the default configuration very reasonable. Here is a selected list of
the features you will get by installing this plugin:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p class="first"&gt;python-aware motions: &lt;tt class="docutils literal"&gt;M&lt;/tt&gt; for the current method, &lt;tt class="docutils literal"&gt;C&lt;/tt&gt; for the current
class. For example, &lt;tt class="docutils literal"&gt;daM&lt;/tt&gt; will delete the current method.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;python-aware code folding. I don't like to have all my code folded when
I open a file so I set the &lt;tt class="docutils literal"&gt;foldlevelstart&lt;/tt&gt; to 99. I also set simpler
shortcuts to toggle code-folding. Here is the related section of my
&lt;tt class="docutils literal"&gt;.vimrc&lt;/tt&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;foldlevelstart&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="m"&gt;99&lt;/span&gt;
&lt;span class="nb"&gt;nnoremap&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;Space&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; za
&lt;span class="nb"&gt;nnoremap&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;S&lt;span class="p"&gt;-&lt;/span&gt;Space&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; zA
&lt;span class="nb"&gt;vnoremap&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;S&lt;span class="p"&gt;-&lt;/span&gt;Space&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; zA
&lt;/pre&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;code completion/analysis/refactoring with Rope. The most useful shortcuts are
described &lt;a class="reference external" href="https://github.com/klen/python-mode#id21"&gt;here&lt;/a&gt;. See &lt;tt class="docutils literal"&gt;:help ropevim.txt&lt;/tt&gt; for more advanced commands.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;&lt;a class="reference external" href="http://www.logilab.org/857"&gt;pylint&lt;/a&gt; checking of your code. By default, pylint is run on your files
every time you save them. I find this quite cumbersome so I disabled it with:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;g&lt;/span&gt;:pymode_lint_write &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;and prefer to run it manually with the &lt;tt class="docutils literal"&gt;PyLint&lt;/tt&gt; command.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Also, note that Python being a dynamic and weakly-typed language, it is hard to
get good auto-completion. I have read some good things about &lt;a class="reference external" href="https://github.com/davidhalter/jedi-vim"&gt;jedi-vim&lt;/a&gt;
auto-completion. If you want to try it out, you simply need to install this
plugin after disabling rope's auto-completion:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;g&lt;/span&gt;:pymode_rope_vim_completion &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div class="line-block"&gt;
&lt;div class="line"&gt;&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;A nice thing about Python being an interpreted language is that it allows you
to quickly test a snippet, update the definition of a function into an already
loaded environment, etc. The python-mode plugin gives you the &lt;tt class="docutils literal"&gt;&amp;lt;Leader&amp;gt;r&lt;/tt&gt;
shortcut to run the current file through Python, but if you want something more
fine-grained, I recommend the &lt;a class="reference external" href="https://github.com/ivanov/vim-ipython"&gt;vim-ipython&lt;/a&gt; plugin. This plugin is still very
rudimentary, but it already gives you the ability to send Python code to
a running IPython kernel. Here is how I use it:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;launch a terminal window and put it side-to-side with my Vim window&lt;/li&gt;
&lt;li&gt;launch an IPython console in the terminal window with the &lt;tt class="docutils literal"&gt;ipython console&lt;/tt&gt;
command (this will also launch an IPython kernel)&lt;/li&gt;
&lt;li&gt;run the &lt;tt class="docutils literal"&gt;:IPython&lt;/tt&gt; command inside my Vim window to connect to the running
IPython kernel&lt;/li&gt;
&lt;li&gt;whenever I want to send some code (e.g. a function) and make it available to
the IPython instance, I select it (e.g with &lt;tt class="docutils literal"&gt;vaM&lt;/tt&gt;) and use the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;&amp;lt;Ctrl-s&amp;gt;&lt;/span&gt;&lt;/tt&gt;
shortcut.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;One thing which is crucially missing is getting feedback from IPython back to
Vim: for example, displaying error messages if the sent code is faulty or
displaying python's output on the code execution.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="conclusion"&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Here are the commands to install all the plugins mentioned in this article:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c"&gt;# if using Pathogen, run these commands in ~/.vim/bundle&lt;/span&gt;

git clone https://github.com/scrooloose/nerdtree.git
git clone https://github.com/klen/python-mode.git
git clone https://github.com/kien/ctrlp.vim.git
git clone https://github.com/kien/rainbow_parentheses.vim.git
git clone https://github.com/majutsushi/tagbar.git
git clone https://github.com/vim-scripts/sessionman.vim.git
git clone https://github.com/tpope/vim-surround.git
git clone https://github.com/ivanov/vim-ipython.git
git clone https://github.com/tomasr/molokai.git
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And the options to put in your &lt;tt class="docutils literal"&gt;.vimrc&lt;/tt&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;execute pathogen#infect&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;filetype&lt;/span&gt; plugin indent &lt;span class="k"&gt;on&lt;/span&gt;
&lt;span class="nb"&gt;syntax&lt;/span&gt; &lt;span class="k"&gt;on&lt;/span&gt;

&lt;span class="c"&gt;&amp;quot; you probably want more options here, see for example this excellent article&lt;/span&gt;
&lt;span class="c"&gt;&amp;quot; http://stevelosh.com/blog/2010/09/coming-home-to-vim/&lt;/span&gt;

&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;foldlevelstart&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="m"&gt;99&lt;/span&gt;
&lt;span class="nb"&gt;nnoremap&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;Space&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; za
&lt;span class="nb"&gt;nnoremap&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;S&lt;span class="p"&gt;-&lt;/span&gt;Space&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; zA
&lt;span class="nb"&gt;vnoremap&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;S&lt;span class="p"&gt;-&lt;/span&gt;Space&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; zA

&lt;span class="nb"&gt;nnoremap&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;F1&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; :NERDTreeToggle&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;cr&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nb"&gt;nnoremap&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="k"&gt;b&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; :CtrlPBuffer&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;cr&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nb"&gt;nnoremap&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;F2&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; :TagbarToggle&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;cr&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nb"&gt;nnoremap&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;F3&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; :RainbowParenthesesToggleAll&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;cr&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;g&lt;/span&gt;:pymode_lint_write &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
</summary></entry><entry><title>How I solved my password problem</title><link href="http://thibaut.horel.org/blog/how-i-solved-my-password-problem.html" rel="alternate"></link><updated>2013-03-05T23:00:00+01:00</updated><author><name>Thibaut</name></author><id>tag:thibaut.horel.org/blog,2013-03-05:how-i-solved-my-password-problem.html</id><summary type="html">&lt;p&gt;Everybody knows that &lt;em&gt;password reuse&lt;/em&gt; (using the same password on different
services, websites, etc.) is a bad habit from the point of view of security.
The reason is simple: if a single website where your password is used gets
compromised, then somebody could get access to your password and consequently
get access to your account on all the websites where you use the same password.
You can get a full description of this phenomenon on &lt;a class="reference external" href="http://xkcd.com/792/"&gt;XKCD&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;On a less funny note, there have been &lt;a class="reference external" href="http://abcnews.go.com/blogs/technology/2012/07/yahoo-password-breach-includes-gmail-hotmail-and-aol-users/"&gt;countless&lt;/a&gt; &lt;a class="reference external" href="http://techcrunch.com/2009/12/14/rockyou-hack-security-myspace-facebook-passwords/"&gt;stories&lt;/a&gt; of password breaches
where password databases of major websites got compromised. These incidents
revealed that very often, these websites do not follow even the very basic
security practices to protect their user passwords.&lt;/p&gt;
&lt;p&gt;Despite, having been well aware of this fact for years, I have never really
taken the problem seriously, barely using a few different passwords depending
on the level of trust I had in the websites. About one year ago, my Gmail
account has been hacked and used to send spam emails to my contact list: it is
a very unpleasant experience when you are being asked by one of your colleague
to explain the “funny email” that was sent on your behalf... This is when
I decided to have only single-purpose passwords.  However, it quickly became
apparent to me that I would need a proper set of tools to handle them.  My
requirements for this set of tools were the following:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;the core of the solution had to be open-source: you can read &lt;a class="reference external" href="http://www.schneier.com/crypto-gram-9909.html#OpenSourceandSecurity"&gt;Bruce
Schneier's opinion&lt;/a&gt; about why open-source is good for security.&lt;/li&gt;
&lt;li&gt;there had to be real-time synchronization of my passwords on the different
machines I use.&lt;/li&gt;
&lt;li&gt;there had to be a way to access my passwords on my phone, in the case where
I need a password on a machine I do not use regularly.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The solution I found, which I have now been happily using for some time, is the
combo: KeePassX + Dropbox + KeePassDroid.&lt;/p&gt;
&lt;div class="section" id="keepassx"&gt;
&lt;h2&gt;KeePassX&lt;/h2&gt;
&lt;p&gt;Here is the description of KeePassX you can find on its official &lt;a class="reference external" href="http://www.keepassx.org/"&gt;website&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;KeePassX is an application for people with extremly high demands on secure
personal data management. It has a light interface, is cross platform and
published under the terms of the GNU General Public License.&lt;/p&gt;
&lt;p&gt;KeePassX saves many different information e.g. user names, passwords, urls,
attachments and comments in one single database. For a better management
user-defined titles and icons can be specified for each single entry.
Furthermore the entries are sorted in groups, which are customizable as
well. The integrated search function allows to search in a single group or
the complete database.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The idea behind KeePassX is simple: all your passwords are stored in a database
which is encrypted by your &lt;em&gt;master password&lt;/em&gt; (a password you choose when
creating the database). Every time you launch KeePassX, the password has to be
typed to unlock the database. Once it is unlocked, you have a nice and simple
interface to add/delete/modify password, organize them in different subgroups,
etc.&lt;/p&gt;
&lt;div class="figure"&gt;
&lt;a class="reference external image-reference" href="static/images/keepassx.png"&gt;&lt;img alt="static/images/keepassx.png" src="static/images/keepassx.png" /&gt;&lt;/a&gt;
&lt;p class="caption"&gt;KeePassX interface&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Let me mention here the killer-feature of KeePassX, &lt;em&gt;autofill&lt;/em&gt;: it allows you
to automatically fill your username and password in a login form by pressing
a global shortcut. KeePassX decides which password to use on which website
based on the window title that you associate with the password when creating
it.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="dropbox"&gt;
&lt;h2&gt;Dropbox&lt;/h2&gt;
&lt;p&gt;The password database being just a single file (with extension .kdb), the
synchronization of my passwords across several computers was really easy to
setup: I just added the database file to my Dropbox folder.&lt;/p&gt;
&lt;p&gt;I am not fully satisfied by this part of the solution and would feel more
comfortable if I were not relying on a third-party to store my passwords.
However, the database file which is hosted by Dropbox is encrypted by my master
password and I can live with this at the moment. Yet, I would be happy to find
an open-source synchronization tool which stands comparison with Dropbox.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="keepassdroid"&gt;
&lt;h2&gt;KeepassDroid&lt;/h2&gt;
&lt;p&gt;Finally, getting access to my passwords on my phone simply involved installting
two applications found on Google Play (my phone runs Android): &lt;a class="reference external" href="https://play.google.com/store/apps/details?id=com.android.keepass"&gt;KeePassDroid&lt;/a&gt;
and &lt;a class="reference external" href="https://play.google.com/store/apps/details?id=com.dropbox.android"&gt;Dropbox&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The Dropbox app will take care of getting your password database file on your
phone memory. Then, you can link the file to the KeePassDroid app by following
these steps:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;in the Dropbox app, navigate to your password database file and try to open
it.&lt;/li&gt;
&lt;li&gt;if you have the KeePassDroid app installed, you will have the option to open
the file with it. Choose this option.&lt;/li&gt;
&lt;li&gt;on the first screen presented to you, check the option &amp;quot;Use this as my
default database&amp;quot; and type in your password.&lt;/li&gt;
&lt;li&gt;you should now be able to navigate through your password database.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you are an iPhone user, &lt;a class="reference external" href="https://itunes.apple.com/en/app/ikeepass/id299697688?mt=8"&gt;iKeePass&lt;/a&gt; should do the job.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="conclusion"&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;I am not claiming that my solution is the perfect way to manage your passwords:
this is just the solution I currently use, and it has worked well for me until
now.&lt;/p&gt;
&lt;p&gt;I know that some people have been successfully using &lt;a class="reference external" href="https://lastpass.com/"&gt;LastPass&lt;/a&gt;: the approach is
browser-based and very different from the one described in this article, but
I would be interested to find a comparison of the two solutions from the point
of view of usability.&lt;/p&gt;
&lt;/div&gt;
</summary></entry><entry><title>Good practices in Python</title><link href="http://thibaut.horel.org/blog/good-practices-in-python.html" rel="alternate"></link><updated>2013-01-20T17:34:00+01:00</updated><author><name>Thibaut</name></author><id>tag:thibaut.horel.org/blog,2013-01-20:good-practices-in-python.html</id><summary type="html">&lt;p&gt;This post is a collection of various facts about Python:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;common mistakes that I encounter frequently when reading code written by
myself or other people.&lt;/li&gt;
&lt;li&gt;specific features of the Python language that are not very well-known and that
I think should be used more.&lt;/li&gt;
&lt;li&gt;general recommendations regarding Python.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Please note that I do not consider myself a Python expert, so it is possible
that the following text contains some inaccurate statements.&lt;/p&gt;
&lt;p&gt;Also, due to its very nature, this post is rather unstructured. The table of
contents should help you jumping directly to the part you are interested in.&lt;/p&gt;
&lt;div class="contents local topic" id="contents"&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference internal" href="#list-comprehensions" id="id1"&gt;List comprehensions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="#the-multiples-faces-of-in" id="id2"&gt;The multiples faces of &lt;tt class="docutils literal"&gt;in&lt;/tt&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="#manipulating-lists-with-atomic-instructions" id="id3"&gt;Manipulating lists with atomic instructions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="#exceptions" id="id4"&gt;Exceptions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="#values-equivalent-to-true-or-false" id="id5"&gt;Values equivalent to &lt;tt class="docutils literal"&gt;True&lt;/tt&gt; or &lt;tt class="docutils literal"&gt;False&lt;/tt&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="#generators" id="id6"&gt;Generators&lt;/a&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class="reference internal" href="#generator-expressions" id="id7"&gt;Generator expressions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="#generator-functions" id="id8"&gt;Generator functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="#built-in-functions" id="id9"&gt;Built-in functions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="#decorators" id="id10"&gt;Decorators&lt;/a&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class="reference internal" href="#a-simple-decorator" id="id11"&gt;A simple decorator&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="#another-example" id="id12"&gt;Another example&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="#classes-with-two-methods" id="id13"&gt;Classes with two methods&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="#pep-8" id="id14"&gt;PEP 8&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="list-comprehensions"&gt;
&lt;h2&gt;&lt;a class="toc-backref" href="#id1"&gt;List comprehensions&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;List comprehensions&lt;/em&gt; give Python users a very concise and powerful syntax to
build a list from another list (or any iterable object). The syntax is the
following:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;result_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;expression&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;original_list&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which means that &lt;tt class="docutils literal"&gt;result_list&lt;/tt&gt; will be a list containing &lt;tt class="docutils literal"&gt;expression(item)&lt;/tt&gt;
(an expression computed from &lt;tt class="docutils literal"&gt;item&lt;/tt&gt;) for each &lt;tt class="docutils literal"&gt;item&lt;/tt&gt; element of
&lt;tt class="docutils literal"&gt;original_list&lt;/tt&gt; for which &lt;tt class="docutils literal"&gt;condition(item)&lt;/tt&gt; (a boolean expression involving
&lt;tt class="docutils literal"&gt;item&lt;/tt&gt;) is &lt;tt class="docutils literal"&gt;True&lt;/tt&gt;. The boolean condition which allows you to filer the list
is optional.&lt;/p&gt;
&lt;p&gt;For example, to compute the list of squares of elements in a list, instead of:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which is particularly inefficient because of the repeated use of the &lt;tt class="docutils literal"&gt;append&lt;/tt&gt;
function, one could use a &lt;em&gt;list comprehension&lt;/em&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c"&gt;# result = [1, 4, 9]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In addition to being shorter, the above code is also faster (around 3x
improvement) because you build the list in one instruction.&lt;/p&gt;
&lt;p&gt;Another example to compute the list of square roots of all non-negative
elements of a list (you could get in big troubles computing the square root of
a negative element):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;math&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sqrt&lt;/span&gt;

&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c"&gt;# result = [2, 3]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The same syntax also exists for dictionaries, this is called &lt;em&gt;dict
comprehensions&lt;/em&gt; (very original, isn't it?). For example, to transform a list of
(name, phone number) pairs into a dictionary, for faster lookup:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Barthes&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;+33 6 29 64 91 12&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;John&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;+001 650 472 4243&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;phone&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;phone&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You can get more details about &lt;em&gt;list comprehensions&lt;/em&gt; on the &lt;a class="reference external" href="http://docs.python.org/tutorial/datastructures.html#list-comprehensions"&gt;dedicated section&lt;/a&gt; of the official documentation.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="the-multiples-faces-of-in"&gt;
&lt;h2&gt;&lt;a class="toc-backref" href="#id2"&gt;The multiples faces of &lt;tt class="docutils literal"&gt;in&lt;/tt&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The &lt;tt class="docutils literal"&gt;in&lt;/tt&gt; keyword has many different meanings and makes Python code so easy to
write that people often forget to use it.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p class="first"&gt;&lt;tt class="docutils literal"&gt;in&lt;/tt&gt; gives a universal syntax to iterate over iterable objects. For
example, to iterate over a list, instead of:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;you could simply write:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;similarly, to iterate over a dictionary, instead of:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;you could write:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;&lt;tt class="docutils literal"&gt;in&lt;/tt&gt; also allows you to test whether an element belongs to some structure:
list, dictionary (or any iterable object), occurrence of a substring inside
a string. For example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;server.log&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;Connected&amp;quot;&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;will return the list of lines from the file &lt;tt class="docutils literal"&gt;server.log&lt;/tt&gt; containing
&lt;tt class="docutils literal"&gt;Connected&lt;/tt&gt; as a substring.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="manipulating-lists-with-atomic-instructions"&gt;
&lt;h2&gt;&lt;a class="toc-backref" href="#id3"&gt;Manipulating lists with atomic instructions&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;More generally, it is advised to avoid iterating over a list with a &lt;tt class="docutils literal"&gt;for&lt;/tt&gt;
loop. &lt;tt class="docutils literal"&gt;for&lt;/tt&gt; loops are slow in Python and writing an operation over a list as
a single instruction allows Python to optimize the execution of the code
internally.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;List comprehensions&lt;/em&gt; often help in replacing an iteration by a single
instruction. Here are a few other functions which can be helpful in this
regard:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p class="first"&gt;&lt;tt class="docutils literal"&gt;join&lt;/tt&gt; can be useful to format a list. For example, to print the list of
words whose first letter is &lt;tt class="docutils literal"&gt;a&lt;/tt&gt; in a list of words. Instead of:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;a&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;&amp;quot; &amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;you could do:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="s"&gt;&amp;quot; &amp;quot;&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;a&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;&lt;tt class="docutils literal"&gt;sum&lt;/tt&gt;, to sum the elements of a list.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;&lt;tt class="docutils literal"&gt;map&lt;/tt&gt;, to apply a given function to all elements in a list. For example to
reverse all the words in a list:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Adam&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;Eve&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;[::&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;# m = [&amp;#39;madA&amp;#39;, &amp;#39;evE&amp;#39;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;slices&lt;/em&gt; are also very useful when it comes to manipulating lists (or sublists)
in blocks. Remember that if &lt;tt class="docutils literal"&gt;l&lt;/tt&gt; is a list (or any iterable)
&lt;tt class="docutils literal"&gt;l[begin:end:step]&lt;/tt&gt; will extract all the elements from index &lt;tt class="docutils literal"&gt;begin&lt;/tt&gt;
(included) to index &lt;tt class="docutils literal"&gt;end&lt;/tt&gt; (excluded) with a step of &lt;tt class="docutils literal"&gt;step&lt;/tt&gt; (this last
parameter being optional).&lt;/p&gt;
&lt;p&gt;If the &lt;tt class="docutils literal"&gt;begin&lt;/tt&gt; parameter is omitted, it is given 0 as default value.
Similarly, the default value of &lt;tt class="docutils literal"&gt;end&lt;/tt&gt; when unspecified is &lt;tt class="docutils literal"&gt;len(l)&lt;/tt&gt; (the
numbers of elements in &lt;tt class="docutils literal"&gt;l&lt;/tt&gt;). A negative value for &lt;tt class="docutils literal"&gt;begin&lt;/tt&gt; or &lt;tt class="docutils literal"&gt;end&lt;/tt&gt; will
be subtracted from the end of the list. For example, to extract all the element
but the last one:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c"&gt;# m = [1, 2]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Using a negative value for the &lt;tt class="docutils literal"&gt;step&lt;/tt&gt; parameter can be useful to walk through
an iterable object in reverse order as shown in the example given above to take
the mirror image of a word:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;dumbo&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;drow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;[::&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c"&gt;# drow = &amp;quot;obmud&amp;quot;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which compensates for the scandalous lack of a &lt;tt class="docutils literal"&gt;reverse&lt;/tt&gt; function for strings
in Python.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="exceptions"&gt;
&lt;h2&gt;&lt;a class="toc-backref" href="#id4"&gt;Exceptions&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Exceptions provide a powerful tool found in many high-level programming
languages which is often under-used. They allow for a less defensive
programming style by handling errors &lt;em&gt;as they appear&lt;/em&gt; instead of making test
&lt;em&gt;beforehand&lt;/em&gt; to prevent them from happening.&lt;/p&gt;
&lt;p&gt;In Python, every time you are trying to execute an illegal operation (&lt;em&gt;e. g.&lt;/em&gt;
trying to access an element outside a list's boundaries, dividing by zero,
etc.) instead of simply crashing the program, Python raises an exception which
can be caught, giving the programmer a last chance to fix the problem before the
program ultimately crashes.&lt;/p&gt;
&lt;p&gt;The syntax to catch exceptions in Python is the following:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="o"&gt;....&lt;/span&gt; &lt;span class="c"&gt;# piece of code potentially raising the exception named Kaboum&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;Kaboum&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="o"&gt;....&lt;/span&gt; &lt;span class="c"&gt;# piece of code to be executed if the above code raises Kaboum&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;For example, if a line of code contains a division by a number which could
seldom be equal to zero, instead of systematically checking that the number
is non zero, it is much more efficient to encapsulate the line within a &lt;tt class="docutils literal"&gt;try
... except ZeroDivisionError:&lt;/tt&gt; to handle specifically the rare cases when the
number will be zero. This is the well-known principle: &lt;em&gt;better ask for
absolution than permission&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Another example, when trying to access an unbound key in a dictionary, Python
raises the &lt;tt class="docutils literal"&gt;KeyError&lt;/tt&gt; exception. This exception can be used to initialize the
value associated with the unbound key. For example, to compute a dictionary of
word counts in a text, you can often find:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;...&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;split&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You could instead use the &lt;tt class="docutils literal"&gt;KeyError&lt;/tt&gt; exception to your advantage to avoid the
systematic &lt;tt class="docutils literal"&gt;if&lt;/tt&gt; test:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;...&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;split&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="ne"&gt;KeyError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The difference with the previous code is that &lt;em&gt;most of the time&lt;/em&gt;, this code
will behave exactly as if the body of the &lt;tt class="docutils literal"&gt;for&lt;/tt&gt; loop only contained the
instruction &lt;tt class="docutils literal"&gt;result[word] += 1&lt;/tt&gt;. This gives a significant speedup compared to
the first code where a test was computed for each iteration of the loop.&lt;/p&gt;
&lt;p&gt;See the &lt;a class="reference external" href="http://docs.python.org/tutorial/errors.html#handling-exceptions"&gt;dedicated page&lt;/a&gt; in the official documentation.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="values-equivalent-to-true-or-false"&gt;
&lt;h2&gt;&lt;a class="toc-backref" href="#id5"&gt;Values equivalent to &lt;tt class="docutils literal"&gt;True&lt;/tt&gt; or &lt;tt class="docutils literal"&gt;False&lt;/tt&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;If &lt;tt class="docutils literal"&gt;test&lt;/tt&gt; is a boolean variable (equal to &lt;tt class="docutils literal"&gt;True&lt;/tt&gt; or &lt;tt class="docutils literal"&gt;False&lt;/tt&gt;), we know
that it is redundant to write:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;instead of:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;More generally, Python has automatic conversion rules from standard types to
booleans. This can be used to shorten the syntax in conditional tests:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p class="first"&gt;as in the vast majority of programming languages, a positive integer is
converted to &lt;tt class="docutils literal"&gt;True&lt;/tt&gt; and zero is converted to &lt;tt class="docutils literal"&gt;False&lt;/tt&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;a string is converted to &lt;tt class="docutils literal"&gt;False&lt;/tt&gt; if and only if it is empty. For example,
to test whether a string &lt;tt class="docutils literal"&gt;title&lt;/tt&gt; is empty, you can simply write:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;instead of:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
     &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;the &lt;tt class="docutils literal"&gt;None&lt;/tt&gt; value, a constant used to initialize unspecified variables, is
converted to &lt;tt class="docutils literal"&gt;False&lt;/tt&gt;. To test that a variable &lt;tt class="docutils literal"&gt;var&lt;/tt&gt; is not equal to
&lt;tt class="docutils literal"&gt;None&lt;/tt&gt;, you can write:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Beware&lt;/strong&gt;, the above code will not allow you to distinguish the case where
&lt;tt class="docutils literal"&gt;var&lt;/tt&gt; is &lt;tt class="docutils literal"&gt;None&lt;/tt&gt; from the case where &lt;tt class="docutils literal"&gt;var&lt;/tt&gt; has a value which is
converted to &lt;tt class="docutils literal"&gt;False&lt;/tt&gt; by Python (for example, an empty string or list).
You need to be careful that this is really what you are trying to test.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="generators"&gt;
&lt;h2&gt;&lt;a class="toc-backref" href="#id6"&gt;Generators&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Generators provide an easy way to create iterator objects (objects over which
you can iterate) and can be created in several ways.&lt;/p&gt;
&lt;div class="section" id="generator-expressions"&gt;
&lt;h3&gt;&lt;a class="toc-backref" href="#id7"&gt;Generator expressions&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;em&gt;Generators expressions&lt;/em&gt; are exactly similar to &lt;em&gt;list comprehensions&lt;/em&gt; except
that the brackets are replaced by parenthesis. Thus, the following code:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;would produce the exact same result had the second line been replaced by:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The difference between the two codes is that in the case where &lt;tt class="docutils literal"&gt;m&lt;/tt&gt; is
defined by a &lt;em&gt;list comprehension&lt;/em&gt; the list is integrally computed and stored
in memory when the variable &lt;tt class="docutils literal"&gt;m&lt;/tt&gt; is defined. On the contrary, when &lt;tt class="docutils literal"&gt;m&lt;/tt&gt; is
defined by a &lt;em&gt;generator expression&lt;/em&gt;, the elements in &lt;tt class="docutils literal"&gt;m&lt;/tt&gt; are generated on
the go &lt;em&gt;when needed&lt;/em&gt;: only when trying to iterate over the variable &lt;tt class="docutils literal"&gt;m&lt;/tt&gt; (as
induced by the call to the &lt;tt class="docutils literal"&gt;join&lt;/tt&gt; function in the above example) are the
elements generated.&lt;/p&gt;
&lt;p&gt;From the speed of execution point of view, both solutions are equivalent: in
the end, each element in &lt;tt class="docutils literal"&gt;m&lt;/tt&gt; will be computed once and only once. From the
memory usage point of view however, generators present a clear advantage:
because the elements are generated dynamically, one at a time, never more than
one element is stored in memory at the same time. In cases when the list is too
big to fit into memory, &lt;em&gt;generators&lt;/em&gt; could be the solution.&lt;/p&gt;
&lt;p&gt;When using a &lt;tt class="docutils literal"&gt;generator expression&lt;/tt&gt; as the argument of a function, Python
allows to drop one pair of parenthesis to make the code more readable. For
example, in the following code:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;the second line can be replaced by:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="generator-functions"&gt;
&lt;h3&gt;&lt;a class="toc-backref" href="#id8"&gt;Generator functions&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;A second way to define a &lt;em&gt;generator&lt;/em&gt; is by writing a function using the special
keyword &lt;tt class="docutils literal"&gt;yield&lt;/tt&gt;. When called, this function will return an iterable object
whose behavior is the following: on each iteration step, the function is
executed until a &lt;tt class="docutils literal"&gt;yield&lt;/tt&gt; instruction is hit. The value following the
&lt;tt class="docutils literal"&gt;yield&lt;/tt&gt; keyword is returned and can be used during the iteration step. The
execution of the function is frozen until the next iteration step.&lt;/p&gt;
&lt;p&gt;For example, let us define the following function:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;min_max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;split&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
            &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="nb"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When called, this function will produce an iterable object. When iterating
over this object, at each iteration, one line of &lt;tt class="docutils literal"&gt;filename&lt;/tt&gt; will be read,
and the minimum and maximum values of this line will be returned when the
&lt;tt class="docutils literal"&gt;yield&lt;/tt&gt; keyword is reached, freezing the execution of the function until
the next iteration.&lt;/p&gt;
&lt;p&gt;Hence, the following code:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sup&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;min_max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inf&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;sup&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mf"&gt;2.&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;is exactly equivalent to:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;split&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="n"&gt;inf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sup&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inf&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;sup&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mf"&gt;2.&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;but allows you to define separately the code which will generate the list of
minimum and maximum values, and the code which makes use of the generated
elements.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="built-in-functions"&gt;
&lt;h3&gt;&lt;a class="toc-backref" href="#id9"&gt;Built-in functions&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Finally, some built-in functions in Python return generator objects. This is
the case of the &lt;tt class="docutils literal"&gt;xrange&lt;/tt&gt; function which behaves exactly as the &lt;tt class="docutils literal"&gt;range&lt;/tt&gt;
function. The difference is that &lt;tt class="docutils literal"&gt;range&lt;/tt&gt; computes a list of integers whereas
&lt;tt class="docutils literal"&gt;xrange&lt;/tt&gt; defines a generator object generating the elements on the go, one at
a time. A call to &lt;tt class="docutils literal"&gt;range(1000000000)&lt;/tt&gt; might induce a memory error on your
machine (depending on your memory capacity), but you will be fine using
&lt;tt class="docutils literal"&gt;xrange&lt;/tt&gt;, both calls being equivalent for iteration purposes. It is almost
always more suitable to use &lt;tt class="docutils literal"&gt;xrange&lt;/tt&gt; instead of &lt;tt class="docutils literal"&gt;range&lt;/tt&gt; and in
Python 3.x &lt;tt class="docutils literal"&gt;xrange&lt;/tt&gt; has even been renamed to &lt;tt class="docutils literal"&gt;range&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;Read more about generators on the &lt;a class="reference external" href="http://docs.python.org/tutorial/classes.html#generators"&gt;official documentation&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="decorators"&gt;
&lt;h2&gt;&lt;a class="toc-backref" href="#id10"&gt;Decorators&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;Decorators&lt;/em&gt; provide a very powerful way to alter the behavior of a function
without redifining it. The syntax is the following:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nd"&gt;@logging&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the above example, we say that &lt;tt class="docutils literal"&gt;f&lt;/tt&gt; has been &lt;em&gt;decorated&lt;/em&gt; with &lt;tt class="docutils literal"&gt;logging&lt;/tt&gt;.
&lt;tt class="docutils literal"&gt;logging&lt;/tt&gt; must be a function taking another function as an argument. The
result of this decoration is equivalent to this piece of code:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;logging&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which means that by decorating &lt;tt class="docutils literal"&gt;f&lt;/tt&gt;  with &lt;tt class="docutils literal"&gt;logging&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;f&lt;/tt&gt; now behaves as
the composite function &lt;tt class="docutils literal"&gt;logging(f)&lt;/tt&gt;.&lt;/p&gt;
&lt;div class="section" id="a-simple-decorator"&gt;
&lt;h3&gt;&lt;a class="toc-backref" href="#id11"&gt;A simple decorator&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Imagine that we want the &lt;tt class="docutils literal"&gt;logging&lt;/tt&gt; decorator to &lt;em&gt;log&lt;/em&gt; the calls made to the
function it decorates, by printing them to the standard output. Such
a decorator could be written like this&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;logging&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;aux&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;Calling&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;
        &lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;aux&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Because &lt;tt class="docutils literal"&gt;logging&lt;/tt&gt; could be used to decorate any function, with an arbitrary
number of arguments and keyword arguments, it is necessary to use the generic
syntax &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;aux(*args,&lt;/span&gt; **kwargs)&lt;/tt&gt;. This syntax stores all the arguments passed to
&lt;tt class="docutils literal"&gt;aux&lt;/tt&gt; in a list named &lt;tt class="docutils literal"&gt;args&lt;/tt&gt; and all the keyword arguments in a dictionary
named &lt;tt class="docutils literal"&gt;kwargs&lt;/tt&gt;. Note that the exact same arguments are passed to &lt;tt class="docutils literal"&gt;fun&lt;/tt&gt;,
meaning that from the argument passing perspective, &lt;tt class="docutils literal"&gt;aux&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;fun&lt;/tt&gt; will
behave similarly. The difference being that &lt;tt class="docutils literal"&gt;aux&lt;/tt&gt; logs the call to the
standard output prior to doing the computation made in &lt;tt class="docutils literal"&gt;fun&lt;/tt&gt;: this is how we
expected the decorator to behave.&lt;/p&gt;
&lt;p&gt;To be perfectly rigorous, the previous decorator should have been written like
this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;wraps&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;logging&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nd"&gt;@wraps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;aux&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;Calling&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;
        &lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;aux&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;aux&lt;/tt&gt; is now itself decorated by the &lt;tt class="docutils literal"&gt;wraps&lt;/tt&gt; decorator provided by the
&lt;tt class="docutils literal"&gt;functools&lt;/tt&gt; module. This decorators does some magic to ensure that &lt;tt class="docutils literal"&gt;aux&lt;/tt&gt;
behaves as closely as possible to &lt;tt class="docutils literal"&gt;fun&lt;/tt&gt;. Without this decorator, the
following code:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nd"&gt;@logging&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;would print &lt;tt class="docutils literal"&gt;aux&lt;/tt&gt; to the standard output, instead of the expected &lt;tt class="docutils literal"&gt;f&lt;/tt&gt;. The
&lt;tt class="docutils literal"&gt;wraps&lt;/tt&gt; decorator ensures among other things that the &lt;tt class="docutils literal"&gt;__name__&lt;/tt&gt; attribute
is preserved throughout a decoration.&lt;/p&gt;
&lt;p&gt;Let us further assume that you want to extend the &lt;tt class="docutils literal"&gt;logging&lt;/tt&gt; decorator to not
only log the calls, but also keep track of how many times the function has been
called.&lt;/p&gt;
&lt;p&gt;You could be tempted to write something like:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;wraps&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;logging&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="nd"&gt;@wraps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;aux&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;{0} has been called {1} times&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;aux&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;However, if you apply this decorator to some function and then call it, you
will get an angry face from Python complaining that the variable &lt;tt class="docutils literal"&gt;a&lt;/tt&gt; is
unbound. The problem comes from this line:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, Python thinks you are redefining the variable &lt;tt class="docutils literal"&gt;a&lt;/tt&gt; and forgets about its
previous definition. As a consequence, when reaching the &lt;tt class="docutils literal"&gt;a + 1&lt;/tt&gt; part, &lt;tt class="docutils literal"&gt;a&lt;/tt&gt;
is no longer defined, causing the error. This is a current limitation of Python
2: local variables that have been defined outside the current scope are read-only.&lt;/p&gt;
&lt;p&gt;A standard way to circumvent this limitation is to use a mutable structure for
&lt;tt class="docutils literal"&gt;a&lt;/tt&gt;: &lt;tt class="docutils literal"&gt;a&lt;/tt&gt; itself cannot be redefined, but the structure it is pointed to
can. Using this, the previous example can be rewritten as:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;wraps&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;logging&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="nd"&gt;@wraps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;aux&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;{0} has been called {1} times&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
        &lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;aux&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;where &lt;tt class="docutils literal"&gt;a&lt;/tt&gt; points to a list of length 1 storing the number of calls at its
first position.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="another-example"&gt;
&lt;h3&gt;&lt;a class="toc-backref" href="#id12"&gt;Another example&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;A common example which is often used to illustrate decorators in Python is
&lt;a class="reference external" href="http://en.wikipedia.org/wiki/Memoization"&gt;memoization&lt;/a&gt;: when a function is
computation-heavy but often called using the same arguments, you can save
a lot of time by caching past results returned by the function.&lt;/p&gt;
&lt;p&gt;This idea can be nicely implemented in Python using a decorator. The decorator
will store past results in a dictionary: when the decorated function will be
called, the decorator will perform a lookup in its dictionary to check whether
the function has already been called with the same argument. If the dictionary
already contains an entry for this argument, the associated value is returned.&lt;/p&gt;
&lt;p&gt;Here is how you could write such a decorator for a single argument function:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;wraps&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;memoize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="nd"&gt;@wraps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;aux&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;aux&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Then if &lt;tt class="docutils literal"&gt;f&lt;/tt&gt; is defined like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nd"&gt;@memoize&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="c"&gt;# very long and heavy computation&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;when calling &lt;tt class="docutils literal"&gt;f&lt;/tt&gt; twice with the same argument, you will incur the computation
cost only during the first call, the second call being almost instantaneous.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="classes-with-two-methods"&gt;
&lt;h2&gt;&lt;a class="toc-backref" href="#id13"&gt;Classes with two methods&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Let us briefly recall how classes work in Python. A class is defined like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;decrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;all the methods of a class take as their first argument the instance on which
the method is being called. By convention, this first arguments is always named
&lt;tt class="docutils literal"&gt;self&lt;/tt&gt;. If &lt;tt class="docutils literal"&gt;a&lt;/tt&gt; is an instance of &lt;tt class="docutils literal"&gt;Cipher&lt;/tt&gt;, the instruction
&lt;tt class="docutils literal"&gt;a.decrypt(message)&lt;/tt&gt; is equivalent to &lt;tt class="docutils literal"&gt;decrypt(a, message)&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;The special function &lt;tt class="docutils literal"&gt;__init__&lt;/tt&gt; is the class constructor and is called every
time an instance of the class is created. Its typical use is to initialize some
attributes of the instance. An instance of &lt;tt class="docutils literal"&gt;Cipher&lt;/tt&gt; class can be created like
this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Cipher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;A flaw commonly found in code written by people coming from object-oriented
programing languages is to create classes for everything. This often leads to
classes containing only two methods, one being the &lt;tt class="docutils literal"&gt;__init__&lt;/tt&gt; function. This
is the case in the class written above as an example. By looking to this
example a bit closer, you can see that it is possible to completely get rid of
the class definition: a &lt;tt class="docutils literal"&gt;decrypt&lt;/tt&gt; function taking the key as an  additional
argument is sufficient:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;decrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Some people could object that it still makes sense to use a class in the
example above, if we plan to extend the &lt;tt class="docutils literal"&gt;Cypher&lt;/tt&gt; class in the future, for
example by adding an &lt;tt class="docutils literal"&gt;encrypt&lt;/tt&gt; function. In my opinion, it is better to start
by writing your code as simply as possible. If you really need to extend the
code, then you can start restructuring it and group several related functions
in a class.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="pep-8"&gt;
&lt;h2&gt;&lt;a class="toc-backref" href="#id14"&gt;PEP 8&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;When writing about good practices in Python, it is impossible not to mention
the PEP8. It is a set of recommendations on coding style in Python.  These
recommendations are of course not absolute rules and should be taken as advice.
However, I noticed that following these recommendations generally leads to
greater code readability. Moreover, as many people who code in Python also
follow these recommendations, adopting them reduces the gap between your code
and code written by others: this will save you some time when reading code.&lt;/p&gt;
&lt;p&gt;Here are a few points extracted from the PEP8:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;you should follow English typographic rules: no space before a colon, no
space before a comma, but a space after, etc.&lt;/li&gt;
&lt;li&gt;you should put spaces around operators like the equal sign, plus sign, etc.&lt;/li&gt;
&lt;li&gt;you should try to limit the length of the lines of code to a maximum of 80
characters.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;More details on the &lt;a class="reference external" href="http://www.python.org/dev/peps/pep-0008/"&gt;PEP8 page&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
</summary></entry></feed>