Loading video player…

Understanding Callable Objects in Python

00:00 Callables are objects that can be called with a pair of parentheses. Some callables accept arguments passed between the parentheses when calling; others take no arguments.

00:09 Some of the common callables you’ll see in Python include functions like the abs() function. When called, it takes in a number and returns its absolute value.

00:18 Class constructors like set(). set() returns an object of the set class. As an optional argument, it takes an iterable to convert into a set.

00:27 Without this argument, it returns an empty set. And importantly, you can call instance methods. Consider the .clear() method found on objects of the list class.

00:36 Calling .clear() returns nothing. In fact, its purpose is to modify a list object in place, removing all of its items.

00:44 So as you can see, callables can be super varied, but they all have one thing in common: they use .__call__().

00:52 .__call__() is the special method that controls callable behavior in Python. And one more note on terminology. Special method is another term for dunder methods, and they’re also called magic methods, so called because they let you create classes that seamlessly integrate with Python syntax and built-in language features, like magic.

01:12 Look at this Example class. This is the bare minimum to make callable instances. class Example: def __call__ with the parameter (self): pass.

01:23 First, note that .__call__() is a method defined inside the Example class. And just like any other instance method, it follows the convention of taking self as its first argument.

01:34 This means that instances of Example will be callable. Of course, in this case, nothing would happen because it has no implementation, only the pass statement.

01:43 But that’s enough to not raise an error, because calling instances of classes without .__call__() results in a TypeError.

01:52 Go ahead and pop open the REPL so you can work through a more interactive example.

01:57 Firstly, how can you see if something is callable? One way would be to look directly at it and see if it has a .__call__() using the built-in dir() function.

02:05 Use dir() to examine the built-in abs() function: dir(abs).

02:10 And yeah, the first item is .__call__(), among a bunch of other special methods and attributes. This will work with user-defined functions as well.

02:19 Create a function called greet(), and all it needs to do is print() ("Hello, World!"). def greet(): print("Hello, World!").

02:29 And for readability’s sake, you can check for .__call__() using dir() with the in operator. __call__ in dir(greet) returns True, as expected for a function.

02:42 Of course, you could also just call it: greet() Hello, World!. And while you generally don’t call special methods directly, you can try it with .__call__().

02:52 greet.__call__() also prints Hello, World! It’s the same effect. You can even think of it this way: when you call an object, Python looks for and invokes .__call__() for you, passing along any arguments.

03:08 So how about user-defined classes? Define a super simple Example class. No contents, just a pass statement: class Example: pass. Looking for .__call__() is a bit different with classes.

03:23 The class constructor will be callable, but you won’t find .__call__() when you pass the class into dir().

03:30 __call__ in dir(Example) returns False. Why? Well, first consider that everything in Python is an object. Classes included.

03:40 If you check the type of Example,

03:43 you’ll see it’s actually an object of the type class. I know it’s a bit trippy. The upshot is that by default, user-defined classes become objects of type.

03:53 So when you call them, the .__call__() from type is invoked.

03:58 So if you pass type into dir() and see if it contains .__call__(), that returns True.

04:05 And while dir() is useful for a lot of things and helps to demystify Python’s behavior, if you just want to see if an object is callable, there’s actually a built-in Python function for that.

04:15 Want to guess what it’s called? Yep: callable(). It takes an object as an argument and returns True if it’s callable, False otherwise.

04:24 So callable(abs) returns True.

04:29 callable(greet) returns True. callable(Example) returns True, and even callable(callable) returns True.

04:41 And with an instance of the Example class,

04:44 example_instance = is the result of calling Example(). callable() (example_instance) returns False.

04:54 Something that you can easily confirm by calling it.

04:57 example_instance() raises a TypeError. Example object is not callable. Alright, now you’ve got a solid foundation of how Python handles callables.

05:09 Next lesson, you’ll build upon that foundation by writing concrete examples that use callable instances in practice.

Become a Member to join the conversation.