Reflection: Calling Methods Dynamically by Name

Remco Gerlich scarblac at pino.selwerd.nl
Thu Jan 11 08:47:57 EST 2001


harrc at my-deja.com <harrc at my-deja.com> wrote in comp.lang.python:
> Does Python have the ability to call methods of a class by name?  For
> example, I have a string: "ProcessData" and the class MyClass has a
> method called ProcessData.  Is there a way I can do something like this:
> 
> mc = MyClass()
> str = "ProcessData"
> args = ("Data", 1, 3)

method = getattr(mc, str)
apply(method, args)

> This reflection-esque behavior would be very useful.  Any ideas?

Reflection and dynamic behavior are the things Python excels at.
You can do nearly anything.

-- 
Remco Gerlich



More information about the Python-list mailing list