Calling a function or method by name

Jonathan Gardner jgardn at alumni.washington.edu
Wed Jan 23 09:35:07 EST 2002


I don't even know where to look for the answer to this one. If I have a 
function name, stored in a string, how do I call that function? I have a 
feeling it has something to do with apply() and __dict__, but I don't know 
how to start looking.

In particular, if I have a class with several subclasses, and I want to call 
a method, how is that done?

What is this for? Well, I want my class to respond to keys being pressed. 
Each key will trigger a call to a class method with no arguments (except 
self). I don't want to have a huge if/elif structure because that is slow. I 
want to use a dict. The keys will be the value of the key being pressed. The 
values will be the name of the function to call.

Any help would be greatly appreciated.

Jonathan




More information about the Python-list mailing list