Calling class method by name passed in variable

Sagari temmokan at gmail.com
Fri May 23 02:26:09 EDT 2008


Greetings,

Can someone suggest an efficient way of calling method whose name is
passed in a variable?

Given something like:

class X:
#...
  def a(self):
# ...

  def b(self):
# ...

#...

x = X()
#...
v = 'a'

How do I call the method of x  whose name is stored in v?

PHP code for this would be:

class X {
  function a() {
  }
}

$x = new X();
$v = 'a';
$x->$v();

I need a solution for Python. Could you suggest anything?

The task it to call a function whose name is taken from user-supplied
input.

Thanks.



More information about the Python-list mailing list