dynamic call of a function

Paul Rubin phr-n2001d at nightsong.com
Fri Oct 19 05:29:38 EDT 2001


anthony harel <anthony.harel at c-s.fr> writes:
> I have got a string that contains the name of the function I
> want to call but I don't want to do something like this :
> 
> if ch == "foo" :
>     self.foo( )
> elif ch == "bar"
>     self.bar( )
> ....
> 
> Is it possible to do sommething like that :
> ch = "foo"
> apply(ch, ( ))???

Try this:

   ch = "foo"
   func = getattr(self, ch)
   func ()

Of course you could also just say getattr(self,"foo")().



More information about the Python-list mailing list