333

Now that it's clear what a metaclass is, there is an associated concept that I use all the time without knowing what it really means.

I suppose everybody made once a mistake with parenthesis, resulting in an "object is not callable" exception. What's more, using __init__ and __new__ lead to wonder what this bloody __call__ can be used for.

Could you give me some explanations, including examples with the magic method ?

martineau
  • 99,260
  • 22
  • 139
  • 249
e-satis
  • 515,820
  • 103
  • 283
  • 322
  • 11
    related: [Python internals: how callables work](http://eli.thegreenplace.net/2012/03/23/python-internals-how-callables-work/) – jfs Mar 23 '12 at 14:46

12 Answers12

328

A callable is anything that can be called.

The built-in callable (PyCallable_Check in objects.c) checks if the argument is either:

  • an instance of a class with a __call__ method or
  • is of a type that has a non null tp_call (c struct) member which indicates callability otherwise (such as in functions, methods etc.)

The method named __call__ is (according to the documentation)

Called when the instance is ''called'' as a function

Example

class Foo:
  def __call__(self):
    print 'called'

foo_instance = Foo()
foo_instance() #this is calling the __call__ method
PKen
  • 101
  • 10
Florian Bösch
  • 26,034
  • 11
  • 45
  • 52
  • 7
    Note that the builtin callable is being removed in Python 3.0 in favor of checking for __call__ – Eli Courtwright Sep 22 '08 at 00:31
  • 17
    @Eli: Hmm that sounds like a **very** bad move. `callable` actually tells you if something is callable or not, while checking for `__call__` tells you nothing; If an object `o` provides `__getattribute__` or `__getattr__`, `hasattr(o, '__call__')` may return True, yet `o` will still not be callable because Python skips `__getattribute__` and `__getattr__` for calls. The only real way left to check if something is callable is thus EAFP. – L̲̳o̲̳̳n̲̳̳g̲̳̳p̲̳o̲̳̳k̲̳̳e̲̳̳ Jul 01 '10 at 23:03
  • 58
    @Longpoke: Just for the record, please see [the documentation for `callable()` in Python 3.x](http://docs.python.org/3/library/functions.html#callable): "_This function was first removed in Python 3.0 and then brought back in Python 3.2._". – Tadeck May 08 '13 at 04:41
  • It seems in python 3.8 only the presence of `tp_call` is checked. See implementation of [PyCallable_Check](https://github.com/python/cpython/blob/master/Objects/object.c#L1432), it's 3 lines. – Michele Piccolini May 28 '20 at 13:38
  • @MichelePiccolini It's been that way for over a decade actually, but it still works to do what it's documented to do, which is to check if something is callable or not. When Python 3 was still young they changed the implementation, and now objects with a `__call__` method just always have `tp_call` set as well. I'm not sure when "all callables have `tp_call`" was implemented, but the `PyCallable_Check` change happened in back in August *2006*: https://github.com/python/cpython/commit/50e9fb9e2d6b4b12524116ab775ac6543e4a5332#diff-ba56d44ce0dd731d979970b966fde9d8dd15d12a82f727a052a8ad48d4a49363 – mtraceur Jan 29 '21 at 19:11
  • To be precise, the old `PyCallable_Check` implemented did **_not_** check if something was "an instance of a class with a `__call__` method"! It did a `PyInstance_Check`, which is *different* in that it only detects for *old-style* classes. I think new-style classes always had the `tp_call` attribute if they were callable. I'm gonna suggest an edit to the answer to clear this up. – mtraceur Jan 29 '21 at 19:23
  • Okay, well, the edit queue is full. But if it were me, I would have 1: reversed the order of the two list items; 2: either deleted the parenthetical "(such as in functions, methods etc)" or added ", new-style classes with `__call__`" to that parenthetical; and 3: reworded the other item to say "(only in Python 2) an instance of an old-style class with a `__call__` method). Because the `tp_call` check covers *everything in Python 3 and everything except old-style classes in Python 2*, and so the `tp_call` check is the overwhelmingly common and most relevant case. – mtraceur Jan 29 '21 at 19:41
89

From Python's sources object.c:

/* Test whether an object can be called */

int
PyCallable_Check(PyObject *x)
{
    if (x == NULL)
        return 0;
    if (PyInstance_Check(x)) {
        PyObject *call = PyObject_GetAttrString(x, "__call__");
        if (call == NULL) {
            PyErr_Clear();
            return 0;
        }
        /* Could test recursively but don't, for fear of endless
           recursion if some joker sets self.__call__ = self */
        Py_DECREF(call);
        return 1;
    }
    else {
        return x->ob_type->tp_call != NULL;
    }
}

It says:

  1. If an object is an instance of some class then it is callable iff it has __call__ attribute.
  2. Else the object x is callable iff x->ob_type->tp_call != NULL

Desciption of tp_call field:

ternaryfunc tp_call An optional pointer to a function that implements calling the object. This should be NULL if the object is not callable. The signature is the same as for PyObject_Call(). This field is inherited by subtypes.

You can always use built-in callable function to determine whether given object is callable or not; or better yet just call it and catch TypeError later. callable is removed in Python 3.0 and 3.1, use callable = lambda o: hasattr(o, '__call__') or isinstance(o, collections.Callable).

Example, a simplistic cache implementation:

class Cached:
    def __init__(self, function):
        self.function = function
        self.cache = {}

    def __call__(self, *args):
        try: return self.cache[args]
        except KeyError:
            ret = self.cache[args] = self.function(*args)
            return ret    

Usage:

@Cached
def ack(x, y):
    return ack(x-1, ack(x, y-1)) if x*y else (x + y + 1) 

Example from standard library, file site.py, definition of built-in exit() and quit() functions:

class Quitter(object):
    def __init__(self, name):
        self.name = name
    def __repr__(self):
        return 'Use %s() or %s to exit' % (self.name, eof)
    def __call__(self, code=None):
        # Shells like IDLE catch the SystemExit, but listen when their
        # stdin wrapper is closed.
        try:
            sys.stdin.close()
        except:
            pass
        raise SystemExit(code)
__builtin__.quit = Quitter('quit')
__builtin__.exit = Quitter('exit')
Blacklight Shining
  • 1,258
  • 2
  • 10
  • 26
jfs
  • 346,887
  • 152
  • 868
  • 1,518
  • 10
    I find the example for the __call__ method highly missleading because it mixes it with a recipe for caching and decorators, which add nothing to the understanding of __call__ – Florian Bösch Sep 26 '08 at 16:13
  • @Florian Bösch: It is a real-world example (I've just copy-pasted that code snippet, I didn't write it for the answer). I'm sorry you find it misleading. – jfs Sep 26 '08 at 22:25
  • 1
    J.F. Sebastian, an example is good if you can take nothing away anymore without breaking it. Direct your gaze a bit below to my answer to see an example that demonstrates nothing but __call__. – Florian Bösch Sep 27 '08 at 13:08
  • 3
    J.F. Sebastian, also piling more examples you copy&pasted from somewhere else that are not minimal doesn't help. – Florian Bösch Sep 27 '08 at 13:10
  • 1
    @Florian Bösch: Real working code is better then artificial examples. Good programming books contain practical examples: K&R "The C Programming Language", "The UNIX Programming Environment", GoF "Design Patterns", esr "The Art of Unix Programming", etc. – jfs Sep 27 '08 at 18:45
  • 20
    @J.F. Sebastian: It's BS that more life-like examples are better. I could show you life-like code that would make you weep as an example. Simple examples work too, and they work better to illustrate something because they don't distract. – Florian Bösch Sep 28 '08 at 22:32
  • 5
    You are explaining what's a callable, but you gave an example how to use callable objects to define a decorator. I know it's a typical usage of __callable__ but this can confuse readers who just want to know what is callable and how to use __callable__. I'd prefer @Florian Bösch's answer. – KFL Mar 06 '12 at 19:01
  • 2
    @Kay: I also like the @Florian Bösch's answer (in its current form). btw, a decorator is *not* a typical usage of a "callable". The most typical "callables" are functions/methods such as `def f(): ...`, and class objects such as `class C: ...` i.e., `f`, `''.strip`, `len`, and `C` all are callable. Instances that have a `__call__()` method in their class are relatively rare. – jfs Mar 06 '12 at 20:30
  • What does this mean ```ret = self.cache[args] = self.function(*args)```. I can't wrap my mind around this idea of using *=* more than once in an expression. – maddypie Aug 18 '20 at 09:22
  • 1
    @maddypie: it just means that both `ret` and `self.cache[args]` refer to the result of the `self.function(*args)` call. To make sure you understand, try to deduce what is `d` in the statement: `d, k = d[k] = {}, 'a'` (it is just an exercise, don't use such constructions in prod.code) -- hint: the value on the right is evaluated first, the assignment happens left to right. – jfs Aug 18 '20 at 16:19
  • @jfs: you explained precisly. If I am correct, ```d``` here is first assigned to ```{}```. But then ```d``` is made into a ```dict```, ```d={'a': ({}, 'a')}```. – maddypie Aug 18 '20 at 16:48
  • @jfs: this is mysterious. ```d``` returned an infinitely nested dictionary! – maddypie Aug 19 '20 at 05:19
  • 1
    @maddypie to solve the mystery, it is enough to count how many dictionaries objects are created in the statement—exactly *one* (`{}` literal creates it) i.e., `d` is self-referential: `d = {k: (d, k)}` (note: `d` is not an empty dictionary at this point) – jfs Aug 20 '20 at 22:28
42

A callable is an object allows you to use round parenthesis ( ) and eventually pass some parameters, just like functions.

Every time you define a function python creates a callable object. In example, you could define the function func in these ways (it's the same):

class a(object):
    def __call__(self, *args):
        print 'Hello'

func = a()

# or ... 
def func(*args):
    print 'Hello'

You could use this method instead of methods like doit or run, I think it's just more clear to see obj() than obj.doit()

Andrea Ambu
  • 34,172
  • 14
  • 51
  • 76
39

Let me explain backwards:

Consider this...

foo()

... as syntactic sugar for:

foo.__call__()

Where foo can be any object that responds to __call__. When I say any object, I mean it: built-in types, your own classes and their instances.

In the case of built-in types, when you write:

int('10')
unicode(10)

You're essentially doing:

int.__call__('10')
unicode.__call__(10)

That's also why you don't have foo = new int in Python: you just make the class object return an instance of it on __call__. The way Python solves this is very elegant in my opinion.

hcalves
  • 2,128
  • 1
  • 20
  • 16
  • You're essentially doing `type(int).__call__(int, '10')` and `type(unicode).__call__(unicode, '10')`. Dunders are always called on their class, not through the instance. And they never go through the metaclass either. For most cases that's just a nitpick, but it matters sometimes. – Mad Physicist Sep 19 '18 at 03:24
11

A Callable is an object that has the __call__ method. This means you can fake callable functions or do neat things like Partial Function Application where you take a function and add something that enhances it or fills in some of the parameters, returning something that can be called in turn (known as Currying in functional programming circles).

Certain typographic errors will have the interpreter attempting to call something you did not intend, such as (for example) a string. This can produce errors where the interpreter attempts to execute a non-callable application. You can see this happening in a python interpreter by doing something like the transcript below.

[nigel@k9 ~]$ python
Python 2.5 (r25:51908, Nov  6 2007, 15:55:44) 
[GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 'aaa'()    # <== Here we attempt to call a string.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
>>> 
ConcernedOfTunbridgeWells
  • 59,622
  • 15
  • 138
  • 193
10

__call__ makes any object be callable as a function.

This example will output 8:

class Adder(object):
  def __init__(self, val):
    self.val = val

  def __call__(self, val):
    return self.val + val

func = Adder(5)
print func(3)
MvdD
  • 17,926
  • 5
  • 51
  • 83
7

Quite simply, a "callable" is something that can be called like a method. The built in function "callable()" will tell you whether something appears to be callable, as will checking for a call property. Functions are callable as are classes, class instances can be callable. See more about this here and here.

Joe Skora
  • 13,947
  • 5
  • 34
  • 38
5

In Python a callable is an object which type has a __call__ method:

>>> class Foo:
...  pass
... 
>>> class Bar(object):
...  pass
... 
>>> type(Foo).__call__(Foo)
<__main__.Foo instance at 0x711440>
>>> type(Bar).__call__(Bar)
<__main__.Bar object at 0x712110>
>>> def foo(bar):
...  return bar
... 
>>> type(foo).__call__(foo, 42)
42

As simple as that :)

This of course can be overloaded:

>>> class Foo(object):
...  def __call__(self):
...   return 42
... 
>>> f = Foo()
>>> f()
42
Armin Ronacher
  • 30,242
  • 12
  • 62
  • 69
3

It's something you can put "(args)" after and expect it to work. A callable is usually a method or a class. Methods get called, classes get instantiated.

Kevin Conner
  • 8,437
  • 4
  • 41
  • 50
3

To check function or method of class is callable or not that means we can call that function.

Class A:
    def __init__(self,val):
        self.val = val
    def bar(self):
        print "bar"

obj = A()      
callable(obj.bar)
True
callable(obj.__init___)
False
def foo(): return "s"
callable(foo)
True
callable(foo())
False
Bojan B
  • 1,882
  • 4
  • 16
  • 24
Ravi Singh
  • 55
  • 1
  • 1
    Are you sure `callable(obj.__init___)` doesn't have an extra underscore (as in AttributeError)? If it doesn't, are you sure the answer isn't `True` for that one? – Mad Physicist Sep 19 '18 at 03:34
2

callables implement the __call__ special method so any object with such a method is callable.

cobie
  • 5,869
  • 8
  • 34
  • 58
1

Callable is a type or class of "Build-in function or Method" with a method call

>>> type(callable)
<class 'builtin_function_or_method'>
>>>

Example: print is a callable object. With a build-in function __call__ When you invoke the print function, Python creates an object of type print and invokes its method __call__ passing the parameters if any.

>>> type(print)
<class 'builtin_function_or_method'>
>>> print.__call__(10)
10
>>> print(10)
10
>>>

Thank you. Regards, Maris

maris
  • 532
  • 5
  • 6
  • 2
    Some of the info here is straight up wrong. E.g. "When you invoke the `print` function, Python creates an object of type print and invokes its method `__call__`". Python does not create a print object. It just calls something equivalent to `type(print).__call__(print, *args, **kwargs)`. And the first sentence doesn't make much sense. You appear to be confusing a callable object and "callable" the function. – Mad Physicist Sep 19 '18 at 03:30