Calling a method using an argument

Duncan Booth duncan.booth at invalid.invalid
Thu Feb 3 11:11:55 EST 2005


Diez B. Roggisch wrote:

> If you have more args, do this:
> 
> def test(self, *args):
>     return getattr(self, args[0])(*args[1:])
> 

This would be cleaner written as:

def test(self, method, *args):
    return getattr(self, method)(*args)

and for complete generality:

def test(self, method, *args, **kw):
    return getattr(self, method)(*args, **kw)



More information about the Python-list mailing list