Bruno Desthuilliers <[EMAIL PROTECTED]> wrote in news:[EMAIL PROTECTED]:
> Steven D'Aprano a écrit : > (snip) > >> Instead of doing: > >> if callable(function): function() >> >> you should do: >> >> try: >> function() >> except TypeError: >> pass > > There are time where you may want to know if you have a callable > without calling it... > >> That should work for most uses of callable(), but isn't quite the >> same. (What if function() has side-effects, or is expensive, and you >> want to determine if it is callable, but not actually call it _now_?) > > Indeed. > > The 'correct' replacement would be: > > if hasattr(obj, '__call__'): > # it's a callable > > but I don't find it so Pythonic to have to check for a __magic__ > method. > > It looks like Python devs have decided it is Pythonic, because it is already in the PEP. Same for execfile and the others. And I am not arguing pro or con. I was just wondering about the reasons, as I haven't been able to find anything (neither here nor on the dev group). bests, ./alex -- .w( the_mindstorm )p.
-- http://mail.python.org/mailman/listinfo/python-list