Never been to DZone Snippets before?

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

python : call an unknow method with named params (See related posts)

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

You need to create an account or log in to post comments to this site.


Click here to browse all 4861 code snippets

Related Posts