[Tutor] Iterating over list of functions

ALAN GAULD alan.gauld at btinternet.com
Wed May 20 19:36:17 CEST 2009


> 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. 

Correct. Maintaining synch of indexes between two arrays of data items
is always going to be a risky business.

> The one area you did not address is the area of efficiency. 
> Is this method also the most efficient solution?


It is an efficient solution - I haven't benchmarked it.
But bear in mind that Python uses dictionaries internally to store variables 
and other object references - in cluding functions. Similarly classes in 
Python are implemented as a specialised kind of dictionary. Thus when 
you call a function in Python or access a class feature you are going 
via a dictionary (representing the namespace of the module). Most 
folks seem to find function dispatch fast enough!

Another aspect to consider. If you have a lot of options you are probably 
going to use a search of your options list to find the index before 
applying that index to your list of functions. (If you hard code the 
index then you might as well hard code the function call!) The 
dictionary avoids the need to lookup the index.

> you are a powerful spokesman for 'correct and proper' coding in the 
> Python community and I take  your opinions and commentaries seriously. 

Blush... I have opinions that's true. 
But as for "correct and proper"? -  I usually defer to Kent for that :-)

Alan G.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090520/c494ae0d/attachment-0001.htm>


More information about the Tutor mailing list