[Tutor] random.choice()

John Fouhy john at fouhy.net
Thu Jul 3 01:22:19 CEST 2008


On 03/07/2008, wesley chun <wescpy at gmail.com> wrote:
>  on a related note, currently in Python, there is a callable() Boolean
>  function that returns True if the object's callable and False
>  otherwise. this will be removed for Python 3.x because you can just
>  use hasattr(obj, '__call__').

I believe this also comes down to a preference for EAFP over LBYL
(http://mail.python.org/pipermail/python-list/2003-May/205182.html).

i.e. instead of saying:

if callable(foo):
    foo()
else:
    # do something else

you can instead say:

try:
    foo()
except TypeError:
    # do something else

-- 
John.


More information about the Tutor mailing list