Alex Rivera | Logout

What is a "callable"?

Asked 2008-09-21T15:34:32.720
378

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 ?

Edit
Report

2 Answers

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.

answered 2008-09-21T15:43:20.840
2

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

answered 2012-05-07T21:40:12.383

Your Answer