calling a method using a variable name?

Alan McIntyre fusion at thuule.pair.com
Fri Mar 14 22:20:12 CET 2003


How about this:

myMethod = 'method'
myCall = 'z=X.'+myMethod+'()'
exec(myCall)

Hope this helps,
Alan McIntyre

Patrick Price wrote:
 >
 > Can I call a method of a class using a variable, eg:
 >
 > class X:
 >   def method():
 >      pass
 >
 >
 > z=X."method"()
 >
 > How would I do this?  I need to call different methods (read from a
 > list) without hardcoding them in the program.
 >
 > Thanks in advance,
 >  -Patrick Price
 >  (jp at moment.net)
 >








More information about the Python-list mailing list