matching keyword arguments to specific methods

Paul Rubin phr-n2003b at NOSPAMnightsong.com
Mon Jan 27 18:22:12 EST 2003


"Donnal Walter" <donnalcwalter at yahoo.com> writes:

> Is there a better Python idiom than the following code for calling a method
> by name if and only if the method exists?
> 
>             if key in dir(self):
>                 self.__class__.__dict__[key](self, kw[key])

I don't quite understand your example but I think you want something like:

    if hasattr(self, key):
       getattr(self,key)(whatever)




More information about the Python-list mailing list