Today let's find out what is the purpose of the callable
function and in which versions of python it does not work.
S0-E28/E30 :)
Callable
A callable is a function that checks other methods/functions/classes to find out if that particular element has a __call__
implemented.
Since variables like int or boolean will not have that, making :
callable(1)
or
callable(True)
Will output a "False". Meaning that those elements are not callable
.
But let's check if an object will return true.
class ASimpleClass():
pass
element1 = ASimpleClass()
callable(element1)
No. It does not - why? because it's an Object, not a Callable.
Now Let's find out if a class will be a callable:
class Test():
pass
callable(Test)
And sure thing, it does.
Also for functions it is valid:
def function1():
pass
assert callable(function)
assert not callable(function())
Is there a situation where you would not recommand callable?
Yes. In fact, callable
was removed between 3.0 to 3.2. In 3.2 + it was brought back.
So I would check if we are using 3.0 to 3.2 python. If yes, then I would not use callable. I would then check with code like that:
isinstance(f, collections.Callable)
Acknowledgement
- What is a callable in python
- Callable at 3.6.4 python docs
- Python callable()
- What's new in Python 3.0 - Python 3.1.5 documentation
Thanks!
That's it :) Comment, share or don't :)
If you have any suggestions what I should blog about in the next articles - please give me a hint :)
See you tomorrow! Cheers!
Comments
comments powered by Disqus