calling a method using a variable name?

Erik Max Francis max at alcyone.com
Fri Mar 14 17:32:18 EST 2003


Alan McIntyre wrote:

> How about this:
> 
> myMethod = 'method'
> myCall = 'z=X.'+myMethod+'()'
> exec(myCall)

Better yet, use getattr:

	getattr(X, myMethod)(...)

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ There is nothing wrong with sobriety in moderation.
\__/ John Ciardi
    Physics reference / http://www.alcyone.com/max/reference/physics/
 A physics reference.




More information about the Python-list mailing list