I Spy Code - Python

Call Methods Dynamically

Here is a python example that shows how to call class functions dynamically:

Source: (example.py)

class Foo(object):
  def a(self):
     print("A")
  def b(self):
     print("B")
 
foo = Foo()
funa = getattr(foo, 'a')
funb = getattr(foo, 'b')
funa()
funb()
 

Output:

$ python example.py
A
B


Related Examples

Call Methods Dynamically
Docstring
Initializing Object Variables
Method Arguments
Subclass Inherits Method
Test For Null Object
hqdwppls.php

7