DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Python : Call An Unknow Method With Named Params

09.08.2005
| 4616 views |
  • submit to reddit
        myObject is an instance of a class
myMethod is the name of the method (string)
myArgs is a dict for named arguments

if hasattr(myObject,myMethod):
    try:
        retValue = getattr(myObject,myMethod)(*(),**(myArgs))
    except TypeError:
        # arguments mismatch
else:
    # there is no "myMethod" method in myObject