In Python, a callable is an object that you can call.
When you define a function in Python:
>>> def greet(name): ... print("Hi", name) ...
You'll get a function object:
>>> greet <function greet at 0x7f61693c5940>
You can call a function object by putting parentheses (
()) after it:
>>> greet("Trey") Hi Trey
So, every function in Python is a callable, meaning it's an object that you're able to call.
Functions are not the only callables in Python.
>>> colors = ["green", "purple", "white", "pink"] >>> enumerate(colors) <enumerate object at 0x7f816c0ad400>
We can loop over this
enumerate object to see what's in it.
Looping is really the one thing we can do with an
enumerate object (more on iterating through lists with indexes in Python).
>>> list(enumerate(colors)) [(0, 'green'), (1, 'purple'), (2, 'white'), (3, 'pink')]
When we call
enumerate, we get back an
It turns out,
enumerate isn't actually a function: it's a class!
>>> enumerate <class 'enumerate'>
enumerate a function because we often use the word "function" in a fuzzy way in Python.
In Python we think in terms of duck typing. So if something is a function-like object (meaning it acts like a function) we may simply call it "a function".
enumerate function acts like a function (you call it using parenthesis) which means it's basically a function.
But more properly,
enumerate is a callable, meaning it's an object that can be called.
In Python, functions can be called and classes can be called. So both functions and classes are callables in Python.
But functions and classes aren't the only callables in Python.
Functions and classes are both callables, but you can actually invent your own callable objects too.
We have a class here called
class Person: def __init__(self, name): self.name = name def __call__(self): print("My name is", self.name)
Classes are callable and we call the
Person class to get back an instance of that class:
>>> trey = Person("Trey") >>> trey <__main__.Person object at 0x7fbf9f3331c0>
Person object is also callable!
We can call that
Person object by putting parentheses after it:
>>> trey() My name is Trey
This works because we've implemented a
__call__ method on the
__call__ method to a class makes its class instances callable.
In fact, anything with a
__call__ is a callable and all callables have a
Even functions and classes have a
__call__ method (that's how they're callable):
>>> greet.__call__ <method-wrapper '__call__' of function object at 0x7fbd631cd940> >>> enumerate.__call__ <method-wrapper '__call__' of type object at 0x56322b9cb580>
In Python, a callable is a function-like object, meaning it's something that behaves like a function. Just like with a function, you can use parentheses to call a callable.
Functions are callables in Python but classes are callables too! These are not the only callables, but they're the primary two types of callables that you'll most often see in Python.
For more on callables in Python, see Python's functions are sometimes classes.
Subscribers get customized weekly recommendations for exercises and screencasts and access over 300 screencasts, exercises, and articles.
Save up to $108 per year if you subscribe by November 28.Save on Python screencasts and exercises
Need to fill-in gaps in your Python skills? I send regular emails designed to do just that.
Sign up for my Python tips emails and I'll share my favorite Python insights with you every couple weeks.
Need to fill-in gaps in your Python skills? I send weekly emails designed to do just that.