string to function reference

Igor V. Rafienko igorr at ifi.uio.no
Fri Sep 1 10:20:51 EDT 2000


* Alex Martelli

[snip]

| The call operation needs to be applied to the function object. The
| reference to the function object is in a dictionary, under a key you
| have (the name you hold). What's so "waay to ugly" about fetching
| the function object from that dictionary, then calling it?


I was hoping there was already a built-in function that did that.
Smth. like (symbol-function)


|     thefun=__dict__[s]
|     thefun()
| 
| 
| You can wrap that in a function, of course (warning, untested):
| 
| def callit(funname,*args,dict=None):
|     if not dict: dict=globals()
|     funobj=dict[funname]
|     return apply(funobj,args)
| 
| so you can say, in your case,
| 
|     callit(s)


Right, I could do that. I could also make it scan both globals() and
__builtins__ to be more general (the "GB" part of the LGB scope rule).
I was just looking for a simpler way to do that.


Thanks for the suggestion.


ivr
-- 
"This 'love' intrigues me -- teach me to fake it"
	-- Dr. Zoidberg from "Why must I be a Crustacean in love?"



More information about the Python-list mailing list