[Tutor] Iterating over list of functions

Robert Berman bermanrl at cfl.rr.com
Wed May 20 18:22:58 CEST 2009


Alan,

The emphasis of your reply certainly makes me look at the dictionary
solution as the most 'correct' solution to utilize. Before I change the
code I just implemented, let me make sure I understand exactly what you
are saying and what you are advocating. The 'dictionary of functions' is
the 'best' approach because of simplicity and because it minimizes
chances or errors. The one area you did not address is the area of
efficiency. Is this method also the most efficient solution?

The reason I am trying to pin you are two fold. First, you are a
powerful spokesman for 'correct and proper' coding in the Python
community and I take  your opinions and commentaries seriously. Again,
how efficient is this method and is it truly the 'most pythonesque'
oriented code.

Thanks again,

Robert


On Wed, 2009-05-20 at 16:07 +0100, Alan Gauld wrote:

> "Robert Berman" <bermanrl at cfl.rr.com> wrote
> 
> > Thank you, Christian. This solution was one I was not expecting and am
> > glad to receive it. It is one I will explore in greater detail later.
> 
> A dictionary of functions is the most common way to tackle 
> this fairly common requirement. It combines readability with 
> ease of maintenance, there is no chance of using the wrong 
> index.
> 
> >> Why not use a dictionary to do the heavy lifting for you
> >> 
> >>  >>> import string
> >>  >>> funcs = {1:string.upper, 2:string.lower}
> >>  >>> funcs[1]('this is a simple test')
> >> 'THIS IS A SIMPLE TEST'
> >>  >>> funcs[2]('THIS IS A SIMPLE TEST')
> >> 'this is a simple test'
> 
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090520/41caae44/attachment.htm>


More information about the Tutor mailing list