Can Python do this?

Gerhard Häring gh_pythonlist at gmx.de
Mon Mar 4 13:03:42 EST 2002


Le 04/03/02 à 12:51, Robert Oschler écrivit:
> Hello, Python newbie here.
> 
> Let's say I'd like to build a method name into a string variable (e.g.
> "FuncCall" + "1" to attempt to call "FuncCall1").  Can I then call that
> method by somehow having the interpreter evaluate the string variable into a
> call to the desired method? (I know this is usually done in a language like
> Prolog or Lisp but I'm hoping Python can do it too.)
 
This can easily be done with the built-ins eval and exec. But in 99 % of
all cases, it's a really bad idea to use them. Perhaps you can tell us
what you're trying to accomplish anyway? There's usually a better way
than (ab)using eval/exec.

Gerhard
-- 
This sig powered by Python!
Außentemperatur in München: 5.7 °C      Wind: 2.1 m/s




More information about the Python-list mailing list