dynamic call of a function

John Roth johnroth at ameritech.net
Fri Oct 19 13:55:42 EDT 2001


"anthony harel" <anthony.harel at c-s.fr> wrote in message
news:3BCFE666.C8C6136C at c-s.fr...
> Hi,
>
> Is it possible to make dynamic call of a function whith python ?
>
> 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, ( ))???
>
>
Of course. Put the names and function definitions
into a dictionary. Then you can do a dictionary reference
to get the actual function to pass to 'apply'.

John Roth





More information about the Python-list mailing list