Algorithms as objects?

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Fri Aug 28 04:48:03 EDT 2009


Kreso a écrit :
> I am writing an application that essentially calculates set of numbers,
> say N1, N2, ..., where they can be calculated by several different 
> algorithms. (One should be able to choose the algorithm at run time.) 
> In each algorithm one starts from a set of functions, say f1, f2, ...,
> which are then transformed into some other functions g1(f1, f2, ..),
> g2(f1, f2, ...), ... , then maybe transformed once more and result is 
> obtained by evaluating final functions. 
>   I can naturally think about this as a collection of transformation
> blocks, which have functions as input and output, and which can be
> put together to get the final results. However, I am not sure
> how to program this, especially since one cannot subclass function
> type.

Nope, but you can write your own callable types:


class functor(object):
     def __call__(self):
         print "%s called" % self

(snip)

> I seem to be confused by the fact that I would like to somehow treat
> algorithms as objects

Strategy pattern anyone ?



More information about the Python-list mailing list