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:
| |
But here we won’t cover those things as they are basic Python knowledge.
| |
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:
| |
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!