[Tutor] Still pondering dynamically named functions ...

Kalle Svensson kalle@lysator.liu.se
Sat, 20 Jul 2002 07:11:36 +0200


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[Graeme Andrew]
> My problem is understanding how in Python I can call dynamically at
> runtime the name of a function I have just read from the
> configuration file ... ie assign a function name to a variable and
> then call the function using this variable.

You can use the dictionary returned by globals() like this:

name = read_from_cfg_file()
globals()[name]()

That is assuming that your functions are bound to global names.  For
example, g won't be in globals() here:

def f():
    def g():
        pass

Peace,
  Kalle
- -- 
Kalle Svensson, http://www.juckapan.org/~kalle/
Student, root and saint in the Church of Emacs.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.6 <http://mailcrypt.sourceforge.net/>

iD8DBQE9OPEEdNeA1787sd0RAm7VAJ9xTCr7UztnAalgiLKYe/Ejl/CamACfdsNL
H25CQQjLC30JRHQlFeJP6iA=
=Vc2l
-----END PGP SIGNATURE-----