[Tutor] Still pondering dynamically named functions ...

Graeme Andrew glide@slingshot.co.nz
Sat, 20 Jul 2002 16:38:07 +1200


Hi All,

Sorry but I am really struggling to understand if the follwoing is possib=
le ..=20
I asked before but perhaps I did not ask it very clearly (and I am new to=
=20
python) !!

I have a configuration file that I need to read and extract the names of =
some=20
processes that need to be executed dynamically by the python program. The=
=20
names of these processes in the configuration file are essentially the na=
mes=20
of the functions I want to call .. or classes I want to create.

My problem is understanding how in Python I can call dynamically at runti=
me=20
the name of a function I have just read from the configuration file ... i=
e=20
assign a function name to a variable and then call the function using thi=
s=20
variable.

I know I can use the 'exec function'  but this does not allow me to acces=
s=20
return values or dynamically create a class.=20

The code would kinda looks like this  ...

 # assign a function to variable name ...
 myfunction =3D=3D "function_a"  =20

 myfunction()   -- # this doesn't work I know .. but how ?
 =20
 #or perhaps=20

 my_object =3D myfunction   # create a instance .. calling the constructo=
r ...=20
is it possible somehow ?

If anyone has some suggestions on how to dynamically assign and execute a=
=20
function via a variable as described above it would be most appreciated..=
=2E

Thanks in advance.

Graeme Andrew