Python Callable refers to objects that are able to be called. We know to not use a word in its own definition, so let’s dive deeper.
A callable is either a class with a __call__ method, or else a function. When you call a function or class, you are telling python to execute this piece of code.
You’ll most commonly be able to tell which items are callables because of the () at the end of their name. Ex: pd.DataFrame.max()
. In this case, .max()
is the callable since it is a function being called on the Pandas Dataframe.
Fun fact: Callable is also a python keyword. callable(your_item)
will return True
if your_item
is able to be called
Let’s take a look at a python callable code sample
Python Callable¶
A callable is a function or class that is able to be called. When you call a function or class, you are telling python to execute a piece of code.
Let's take a look at a few callable examples
import numpy as np
x = np.linspace(start=0, stop=10, num=15)
x
# If you call a function, but don't put on the "()", you will just get the function returned to you
x.max
# But calling the function (which is a callable) with the () at the end, you'll run the function
x.max()
Now let's play around with items that are not callables and see what python does
First, lets call .ndim, which will return the number of dimensions to us in numpy
x.ndim
Now let's try 'calling' it
x.ndim()
Oh no! An error. Whenever I see this error, I basically know that I tried 'calling' something that was not able to be called. This is most commonly an oversite on my part.
I need to go back and look at all of my function calls and make sure I know what they are doing.
Check out more Python Vocabulary on our Glossary Page