DEV Community

Discussion on: Call me maybe - an explanations of Python callables

Collapse
 
rhymes profile image
rhymes • Edited

Hi Alexandre, nice intro to callable.

Python definitely leans on EAFP over LBYL as you know :)

A tip: there's an easier way to check if something is a callable that requires zero lines of custom code. The builtin callable() function.

>>> def foo(*args):
...     pass
...
>>> bar = lambda *args: 0
>>> class Foo:
...     def __call__(self, *args):
...         print("hello world!")
...
>>> callable(foo), callable(bar), callable(Foo), callable(Foo()), callable(3)
(True, True, True, True, False)

hope this helps! :-)

Collapse
 
lexplt profile image
Alexandre Plt

Hi!
Thanks for the tip, I had forgotten that this existed :/

I'll add that :)