Today let's find out what is the purpose of the
callable function and in which versions of python it does not work.
A callable is a function that checks other methods/functions/classes to find out if that particular element has a
Since variables like int or boolean will not have that, making :
Will output a "False". Meaning that those elements are
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:
- 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
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!