calling a function indirectly
Jeff Hinrichs
jlh at cox.net
Wed Feb 20 01:24:55 EST 2002
If you wanted to get away from the dangerous eval, you could put your
functions inside of a class and then,
class foo:
def func(self):
return 'hello'
y=foo()
x =getattr(y,"func")
print x()
-Jeff
"Grant Edwards" <grante at visi.com> wrote in message
news:slrna769j8.1hv.grante at tuxtop.visi.com...
> In article <a4v7s6$13tk at r02n01.cac.psu.edu>, Rajarshi Guha wrote:
> > Hi,
> > is it possible to have a variable contain a function name, and use the
> > variable to call the function?
> > An example:
> >
> > def func:
> > some code
> >
> > funcvar = 'func'
> >
> > Now use funcvar (somehow!) to call func
>
> How about this:
>
> def func():
> print "hello"
>
> x = func
>
> x()
>
> It doesn't use the name of the function, but if you really want
> to do it that way you can do this:
>
> def func():
> print "hello"
>
> x = "func"
>
> eval(x)()
>
> --
> Grant Edwards grante Yow! I want a
VEGETARIAN
> at BURRITO to go... with
> visi.com EXTRA MSG!!
More information about the Python-list
mailing list