Call me maybe: an explanation of Python callable

If you have some experience with Python, you must have already seen a TypeError: 'T' object is not callable.

In this article we will try to demystify this kind of errors.

What is a callable?

A callable is anything that can be called, from a function, to a class constructor.

For example, len is a callable because we can write len(object).

Creating a callable

The easiest way is to write a function or a lambda:

1
2
3
4
def foo(*args):
    pass

bar = lambda *args: 0

But here we won’t cover those things as they are basic Python knowledge.

1
2
3
4
5
6
class Foo:
    def __call__(self, *args):
        print("hello world!")

bar = Foo()  # instanciating a Foo object, creating a callable
bar()  # calling it!

We created a basic callable by giving it a __call__ method, and we can check if an object is callable (useful when debugging) by using:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# homemade method to explain EAFP
def is_callable(obj, *args, **kwargs, ***okwargs):
    try:
        obj(*args, **kwargs, ***okwargs)
    except TypeError as e:
        return 'object is not callable' not in str(e)
    return True

# built-in way to check if an object is callable (thanks rhymes for pointing it out): using callable(obj)
>>> callable(foo), callable(bar), callable(Foo), callable(Foo()), callable(3)
(True, True, True, True, False)

Because we can’t use hasattr(obj, '__call__'), since Python is skipping __getattribute__ and __getattr__ when calling something, thus we can only do what’s called EAFP: Easier to ask for forgiveness than permission.


Hopefully this article has helped you, if you have anything to add feel free to leave a comment!

Licensed under CC BY-NC-SA 4.0
Last updated on Aug 20, 2024 12:48 +0200
Built with Hugo
Theme Stack designed by Jimmy