Given a string - execute a function by the same name

Andrew Koenig ark at acm.org
Thu May 8 12:33:19 EDT 2008


<python at bdurham.com> wrote in message 
news:mailman.291.1209400412.12834.python-list at python.org...

> I'm parsing a simple file and given a line's keyword, would like to call
> the equivalently named function.

No, actually, you woudn't :-)  Doing so means that if your programs input 
specification ever changes, you have to rename all of the relevant 
functions.  Moreover, it leaves open the possibility that you might wind up 
calling a function you didn't intend.

The right way to solve this kind of problem is to list all the functions you 
wish to be able to call in this way, and then explicitly define a mapping 
from keywords to the appropriate functions.  Which is exactly what you're 
doing in

> 3. Place all my functions in dictionary and lookup the function to be 
> called





More information about the Python-list mailing list