Calling class method by name passed in variable
Bruno Desthuilliers
bruno.42.desthuilliers at websiteburo.invalid
Fri May 23 08:19:16 EDT 2008
Nick Craig-Wood a écrit :
> Bruno Desthuilliers <bruno.42.desthuilliers at websiteburo.invalid> wrote:
>>> Can someone suggest an efficient way of calling method whose name is
>>> passed in a variable?
>>>
>> method = getattr(obj, 'method_name', None)
>> if callable(method):
>> method(args)
>
> I think that that is needless LBYL...
From experience, it isn't.
> getattr(obj, 'method_name')(args)
>
> Will produce some perfectly good exceptions
The problem is that you can't tell (without reading the exception's
message and traceback etc) if the exception happened in you code or
within the called method.
More information about the Python-list
mailing list