Tuesday, August 21, 2012

New-style Python classes

With Python 2.2 a new type of classes, named new-style classes, was introduced. New-style classes add some convenient functionality to classic classes. New-style classes are recognized by having class object as base class.

Thursday, August 09, 2012

Python caching decorators

Implementing a dynamic programming algorithm is made much easier in Python by using a caching/memorization decorator.

Friday, May 11, 2012

Python's handling of default parameter values

Python's handling of default parameter values becomes tricky when one uses a mutable object (e.g., list or dictionary) as a default value.

Default parameter values are evaluated when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that the same "pre-computed" value is used for each call. If the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified.
This has to be borne in mind when writing recursive Python programs.

Python's splat operator

Python actually has the splat operator (as in Perl or Ruby) that can unpack the arguments out of a list, tuple, or dictionary: func(*args), or func(**kwdargs).

Wednesday, September 07, 2011

The Hungarian algorithm in clustering evaluation

The Hungarian algorithm (aka Kuhn–Munkres algorithm or Munkres assignment algorithm) can solve the assignment problem in polynomial time O(n^3). It can be used to find the optimal mapping from discovered clusters to the ground-truth categories which serves as the basis for some performance measures of clustering.